【问题标题】:attempt to index nil with 'WaitForChild'尝试使用“WaitForChild”索引 nil
【发布时间】:2022-02-23 21:42:48
【问题描述】:

我目前正在编写一个脚本,以便在触摸一个块时,玩家将进入位置。这是

game.Workspace.AUPortal.Glitch4.Position

我需要帮助解决这个错误。

Workspace.A12.MovePlayer:4:尝试使用“WaitForChild”索引 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)

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

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

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

    通过 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)
    

    【讨论】:

    • 我同意,LocalPlayer = nill。但。我只用空值解决了这个问题。现在这个本地玩家 = game.Players:GetPlayerFromCharacter(HitPart.Parent) 给我写错误:GetControls is not a valid member of Player "Players.DanilX448" (这个函数不存在)
    • GetControls 是一个客户端函数。您需要在 LocalScript 中执行此操作或使用 RemoteEvent,或者您可以在不希望他们移动的短时间内锚定 Humanoid。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2021-03-30
    • 2022-12-24
    • 2021-09-04
    • 2022-01-05
    • 2021-08-02
    • 2021-03-16
    相关资源
    最近更新 更多