Lua 用“零”表示零;FindFirstChild”一词;及;WaitForChild“;
我正在一本名为《Roblox Lua:初学者脚本》的书中尝试一个例子。 我犯了一个错误。错误为“Workspace.Part.Script:4:尝试使用'WaitForChild'索引nil”。对于FindFirstChild,我也会遇到同样的错误 这是排行榜脚本:Lua 用“零”表示零;FindFirstChild”一词;及;WaitForChild“;,lua,roblox,Lua,Roblox,我正在一本名为《Roblox Lua:初学者脚本》的书中尝试一个例子。 我犯了一个错误。错误为“Workspace.Part.Script:4:尝试使用'WaitForChild'索引nil”。对于FindFirstChild,我也会遇到同样的错误 这是排行榜脚本: game.Players.PlayerAdded:Connect(function (player) stats = Instance.new("IntValue") stats.Parent
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异常,我不知道为什么。我做了一些研究,但似乎所有我研究的东西都比我的问题复杂一点。当我进入游戏时,领头羊状态确实出现了。列也会显示(点)。有什么想法吗?好的,为了让代码正常工作,您必须更改一些内容,让我来帮助您 正如Alexander所说,您需要在变量中使用局部变量,也需要为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)
对于触摸部分,这里的主要问题是,将始终为零,因为您没有收到来自触摸的参数,而且您可能希望将该脚本放置为服务器端,而不是localscript,并且不使用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)
旁注:始终将变量和非方法函数声明为
local
。这将防止污染全局名称空间、函数之间不必要的交互,还将加快脚本的速度。说明:“此属性仅为本地脚本(以及它们所需的模块描述)定义,因为它们在客户端上运行。对于服务器(脚本对象在其上运行其代码),此属性为零。有关Roblox上游戏联网的更多信息,请参阅Roblox客户端-服务器模型。”你确定这不是你的情况吗?我相信这是我的情况。我去看看。虽然这本书已经出版,我相信他们树立榜样的方式是正确的。我知道有些已经过时了,但这不是。只是一句咆哮。不要浪费你的时间。谢谢你的链接。我会很快检查出来。@AlexanderMashin所以我试了整个本地脚本,并查看了文档,似乎它不起作用。我在角色中添加了一个本地脚本,但它没有运行。你对可能发生的事情有什么想法吗?在stats.Name
之前,你不需要local
。除了实际声明它们之外,你不需要任何本地点或死亡点。虽然我现在正在尝试。谢谢你们。这确实奏效了。我拿出本地的东西进行初始化和点初始化。我还请了当地人帮他们定名字。现在,为什么你会选择WaitForChild而不是FindFirstChild作为角色脚本的统计数据?Alexander你对那些本地人的看法是正确的,我没有支付任何费用lol,很抱歉,我正在编辑它。