尝试用“WaitForChild”索引 nil

发布于 2025-01-09 01:40:29 字数 672 浏览 0 评论 0原文

我目前正在制作一个脚本,以便当触摸块时,玩家将到达位置。这是

游戏.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2025-01-16 01:40:29

我只能假设您正在尝试访问标准脚本内部的LocalPlayer,这是您无法做到的。

您只能访问 LocalScript 内部的 LocalPlayer,因此您会收到 WaitForChild 错误。因为 LocalPlayer 不存在(它为零)。

通过 Touched 事件,您实际上可以获得您正在尝试使用的播放器的引用:

Part.Touched:Connect(function(HitPart)
 local Humanoid = HitPart.Parent:FindFirstChild('Humanoid');
 if (Humanoid) then
  local Player = game.Players:GetPlayerFromCharacter(HitPart.Parent);
  -- You can then use this player reference.
 end
end)

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:

Part.Touched:Connect(function(HitPart)
 local Humanoid = HitPart.Parent:FindFirstChild('Humanoid');
 if (Humanoid) then
  local Player = game.Players:GetPlayerFromCharacter(HitPart.Parent);
  -- You can then use this player reference.
 end
end)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文