尝试用“WaitForChild”索引 nil
我目前正在制作一个脚本,以便当触摸块时,玩家将到达位置。这是
游戏.Workspace.AUPortal.Glitch4.Position
和我在这个错误中需要帮助。
Workspace.A12.MovePlayer:4:尝试使用“WaitForChild”索引 nil
脚本索引 nil:
function onTouched(hit)
local h = hit.Parent:findFirstChild("Humanoid")
local playerMod = require(game:GetService("Players").LocalPlayer:WaitForChild("PlayerModule"))
local controls = playerMod:GetControls()
if h~=nil then
controls:Disable()
pos = game.Workspace.AUPortal.Glitch4.Position
h:MoveTo(pos)
wait()
end
end
script.Parent.Touched:connect(onTouched)
I am currently making a script so that when a block is touched, the player will go to position. It is
game.Workspace.AUPortal.Glitch4.Position
and me need help in this error.
Workspace.A12.MovePlayer:4: attempt to index nil with 'WaitForChild'
Script:
function onTouched(hit)
local h = hit.Parent:findFirstChild("Humanoid")
local playerMod = require(game:GetService("Players").LocalPlayer:WaitForChild("PlayerModule"))
local controls = playerMod:GetControls()
if h~=nil then
controls:Disable()
pos = game.Workspace.AUPortal.Glitch4.Position
h:MoveTo(pos)
wait()
end
end
script.Parent.Touched:connect(onTouched)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只能假设您正在尝试访问标准脚本内部的LocalPlayer,这是您无法做到的。
您只能访问 LocalScript 内部的 LocalPlayer,因此您会收到 WaitForChild 错误。因为 LocalPlayer 不存在(它为零)。
通过 Touched 事件,您实际上可以获得您正在尝试使用的播放器的引用:
I can only assume you are trying to access the LocalPlayer inside of a standard Script which is something you cannot do.
You can only access the LocalPlayer inside of a LocalScript, hence why you're getting an error for the WaitForChild. Because the LocalPlayer does not exist (it's nil).
With the Touched event you can get actually get a reference to the player you're trying use: