使用 "FindFirstChild" 和 "WaitForChild" 获取 NIL
Getting NIL with "FindFirstChild" and "WaitForChild"
我正在尝试一本名为 Roblox Lua 的书上的示例:为初学者编写脚本排行榜 Script-Through。
我收到一个错误。错误是“Workspace.Part.Script:4:尝试使用 'WaitForChild' 索引 nil”。我也遇到了 FindFirstChild 的同样错误。
这是排行榜脚本:
game.Players.PlayerAdded:Connect(function (player)
stats = Instance.new("IntValue")
stats.Parent = player
stats.Name = "leaderstats"
points = Instance.new("IntValue")
points.Parent = stats
points.Name = "Points"
deaths = Instance.new("IntValue")
deaths.Parent = stats
deaths.Name = "Deaths"
end)
这是我创建的硬币部分的脚本:
script.Parent.Touched:Connect(function()
player = game:GetService("Players").LocalPlayer
stats = player:WaitForChild("leaderstats")
points = stats:findFirstChild("Points")
points.Value = points.Value + 1
script.Parent:Destroy()
end)
当你踩到这枚硬币时,它假设在点列中添加一个点。我正在尝试使用 FindFirstChild,但我一直收到 NIL 异常,我无法弄清楚原因。我做了一些研究,但似乎我研究的所有东西都比我的问题复杂一点。当我加载到游戏中时,leaderstats 确实出现了。列也显示为好(点)。有什么想法吗?
好的,您必须更改一些内容才能让代码正常工作,让我来帮助您。
正如亚历山大所说,您需要在变量中使用局部变量,同时声明 int 值的值,建议也使用 Instance.new("IntValue",parent) 而不是使用新行.同样对于 leaderstats,最好使用文件夹而不是值。
game.Players.PlayerAdded:Connect(function (player)
local stats = Instance.new("Folder",player)
stats.Name = "leaderstats"
local points = Instance.new("IntValue",stats)
points.Value = 0
points.Name = "Points"
local deaths = Instance.new("IntValue",stats)
deaths.Value = 0
deaths.Name = "Deaths"
end)
对于接触的部分也是如此,这里的主要问题是总是 nil 因为你没有收到来自接触的参数,你也可能想把那个脚本放在服务器端,而不是本地脚本,而不是使用localplayer 否则对服务器没有影响,谁也拿不到积分
script.Parent.Touched:Connect(function(hit) --hit is the exact part that touch
player = game.Players:FindFirstChild(hit.Parent.Name) --look for the parents name of the part
if player then --detect if what touched the part is a player
local stats = player:WaitForChild("leaderstats")
local points = stats:FindFirstChild("Points")
points.Value = points.Value + 1
script.Parent:Destroy()
end
end)
我正在尝试一本名为 Roblox Lua 的书上的示例:为初学者编写脚本排行榜 Script-Through。 我收到一个错误。错误是“Workspace.Part.Script:4:尝试使用 'WaitForChild' 索引 nil”。我也遇到了 FindFirstChild 的同样错误。
这是排行榜脚本:
game.Players.PlayerAdded:Connect(function (player)
stats = Instance.new("IntValue")
stats.Parent = player
stats.Name = "leaderstats"
points = Instance.new("IntValue")
points.Parent = stats
points.Name = "Points"
deaths = Instance.new("IntValue")
deaths.Parent = stats
deaths.Name = "Deaths"
end)
这是我创建的硬币部分的脚本:
script.Parent.Touched:Connect(function()
player = game:GetService("Players").LocalPlayer
stats = player:WaitForChild("leaderstats")
points = stats:findFirstChild("Points")
points.Value = points.Value + 1
script.Parent:Destroy()
end)
当你踩到这枚硬币时,它假设在点列中添加一个点。我正在尝试使用 FindFirstChild,但我一直收到 NIL 异常,我无法弄清楚原因。我做了一些研究,但似乎我研究的所有东西都比我的问题复杂一点。当我加载到游戏中时,leaderstats 确实出现了。列也显示为好(点)。有什么想法吗?
好的,您必须更改一些内容才能让代码正常工作,让我来帮助您。
正如亚历山大所说,您需要在变量中使用局部变量,同时声明 int 值的值,建议也使用 Instance.new("IntValue",parent) 而不是使用新行.同样对于 leaderstats,最好使用文件夹而不是值。
game.Players.PlayerAdded:Connect(function (player)
local stats = Instance.new("Folder",player)
stats.Name = "leaderstats"
local points = Instance.new("IntValue",stats)
points.Value = 0
points.Name = "Points"
local deaths = Instance.new("IntValue",stats)
deaths.Value = 0
deaths.Name = "Deaths"
end)
对于接触的部分也是如此,这里的主要问题是总是 nil 因为你没有收到来自接触的参数,你也可能想把那个脚本放在服务器端,而不是本地脚本,而不是使用localplayer 否则对服务器没有影响,谁也拿不到积分
script.Parent.Touched:Connect(function(hit) --hit is the exact part that touch
player = game.Players:FindFirstChild(hit.Parent.Name) --look for the parents name of the part
if player then --detect if what touched the part is a player
local stats = player:WaitForChild("leaderstats")
local points = stats:FindFirstChild("Points")
points.Value = points.Value + 1
script.Parent:Destroy()
end
end)