【问题标题】:Roblox Why can't I get Players.LocalPlayer.Character in gameRoblox 为什么我无法在游戏中获取 Players.LocalPlayer.Character
【发布时间】:2020-06-26 23:47:22
【问题描述】:

我一直在关注许多使用的教程:

game.Players.LocalPlayer.Character

但是每次我尝试运行它时,我都会收到一条错误消息:

Players.icrann.PlayerScripts.Script:2:尝试索引字段“字符”(零值)

我正在尝试使用此代码来查找 HumanoidRootPart,以便我可以检查玩家的位置。 game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")

并且还使播放器能够运行,即使我尝试使用工具箱中的脚本,它仍然无法正常工作。 game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 25

在每种情况下,它都会带来相同的错误。 我还在资源管理器中添加了 Players 文件夹的屏幕截图。

提前感谢您的帮助。

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    当您的代码运行时,玩家角色不会加载到游戏中。所以你需要添加等待。

    local Player = game:GetService("Players").LocalPlayer
    local character = Player.Character or Player.CharacterAdded:Wait()
    local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
    -- setting speed
    local Humanoid =  character:WaitForChild("Humanoid")
    if Humanoid then
    Humanoid.WalkSpeed = 25
    end
    

    【讨论】:

    • 它有效,但我认为@Kylaaa 的做法更简单,谢谢
    【解决方案2】:

    我相信您遇到了时间问题。 PlayerScripts 在玩家加入时执行,并且您的角色可能在脚本执行时尚未加载。

    将 LocalScript 移动到 CharacterScripts 中,这将导致脚本在角色加载后触发。

    【讨论】:

      猜你喜欢
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 2021-10-09
      • 2020-11-18
      • 2021-06-09
      • 2022-12-09
      相关资源
      最近更新 更多