'Text' 函数的第三个参数出现问题了(预期是一个字符串,但得到了 nil)

我正在制作一个 Roblox 地图。我编写了一个脚本。请看一下,帮我看看哪里出了问题? 在其他的脚本中都没有问题!你能帮我解决吗?

    local players = game.Players:GetChildren()
    local pupil = players[math.random(0,#players)]
    local James = game.Workspace.James
    local Texte = game.StarterGui.ScreenGui.Maintexzt
    local nameq = game.StarterGui.ScreenGui.Maintexzt.Nameq
    function ontouch(hit)
    if hit.Parent:findFirstChild("Humanoid") then
    print("Trigget working!")
    James.Humanoid.Torso.CFrame = game.Workspace.OutOfhere.CFrame
    script.Parent.CFrame = game.Workspace.SpawnLocation.CFrame
end
end
    script.Parent.Touched:Connect(ontouch)
    function ontouchexit()
Texte.Text = "嗯...我们做到了??"
wait(2)
nameq.Text = players.Name
Texte.Text = "哦,我们到底怎么做到的?"
wait(2)
nameq.Text = James.Name
Texte.Text = "好了,回家吧!"
wait(3)
nameq.Text = players.Name
Texte.Text = "走吧!"
nameq.Text = James.Name
Texte.Text = "去我家吧!"
nameq.Text = players.Name
Texte.Text = "不了"
nameq.Text = James.Name
Texte.Text = "你的选择,你自己承担后果,你可以跟我来,也可以不来"
end
script.Parent.TouchEnded:Connect(ontouchexit)

请帮我看看哪里出了问题,我想要完成地图开发。

点赞
用户2858170
用户2858170
local players = game.Players:GetChildren()

使用 GetChildren() 函数可以返回一个由 Instance 直接子元素所组成的数组。

https://developer.roblox.com/en-us/api-reference/function/Instance/GetChildren

nameq.Text = players.Name

我不是 Roblox 专家,但我认为 players.Name 很可能是 nil

你的代码进一步证明了给这些 Text 属性分配值会调用某个函数,这是我看到的错误消息的原因。

我认为一个玩家数组不会说 "Uh... We did it??",所以我猜想解决你的问题的办法是找到你想要说话的实际玩家并使用他们的名称。

打印 players.Name 并自行了解。

2019-10-14 09:36:52
用户13869224
用户13869224

这可以帮助:

local Workspace     =   game.Workspace or workspace;
local StarterGui    =   game.StarterGui;
local players       =   game:GetService("Players");
-----------------------------------------------------------------------------
local Pupil     =   Players[math.random(0,#Players)];
local James     =   Workspace:FindFirstChild("James");
local Texte     =   StarterGui.ScreenGui:FindFirstChild("Maintexzt");
local nameq     =   StarterGui.ScreenGui.Maintexzt:FindFirstChild("Nameq");
local Target    =   ""; --存储任何接触 TouchPart 的玩家的字符串值
-----------------------------------------------------------------------------
function ontouch(hit)
    if (hit.Parent:FindFirstChild("Humanoid")) then
        print("Trigger working!");
        Target                      =   hit.Parent.Name;    --保存玩家姓名的值
        James.Humanoid.Torso.CFrame =   Workspace.OutOfhere.CFrame;
        script.Parent.CFrame        =   Workspace.SpawnLocation.CFrame;
    end
end
-----------------------------------------------------------------------------
function ontouchexit()
    --将 'players.Name' 替换为存储字符串的 'Target'
    Texte.Text = "Uh... We did it??"
    wait(2)
    nameq.Text = Target
    Texte.Text = "Oh how we make it?"
    wait(2)
    nameq.Text = James.Name
    Texte.Text = "Better, go home!"
    wait(3)
    nameq.Text = Target
    Texte.Text = "Go!"
    nameq.Text = James.Name
    Texte.Text = "But go to my home!"
    nameq.Text = Target
    Texte.Text = "Nope"
    nameq.Text = James.Name
    Texte.Text = "Your choice, Your die, You can follow me, or not"
    Target = ""; --将之前接触 TouchPart 的人的值重置
end
-----------------------------------------------------------------------------
script.Parent.Touched:Connect(ontouch)
script.Parent.TouchEnded:Connect(ontouchexit)

编辑:如果不起作用,请尝试将

function ontouch(hit)
    if (hit.Parent:FindFirstChild("Humanoid")) then

替换为

function ontouch(hit)
    if (hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name == players.Name) then

这会检查接触它的人是否为实际玩家,而不是同样拥有机器人的某些人(NPC)。

2021-10-08 17:43:45