简体   繁体   English

“尝试使用‘MoveDirection’索引 nil”

[英]"attempt to index nil with 'MoveDirection'"

I am making a script which changes the footstep sound of the player but it keeps returning with the error message "attempt to index nil with 'MoveDirection'.我正在制作一个脚本来改变玩家的脚步声,但它不断返回错误消息“尝试使用'MoveDirection'索引nil。

local runService = game:GetService("RunService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local human = character:FindFirstChild("Humanoid")


runService.RenderStepped:Connect(function()
    if human.MoveDirection.Magnitude > 0 then -- walking
        game.SoundService.WalkingSound.Playing = true
    else -- if player not walking
        game.SoundService.WalkingSound.Playing = false
    end
end)

I have tried changing variables and double checking spelling, still returns back with an error message.我已经尝试更改变量并仔细检查拼写,但仍然返回一条错误消息。

any help is appreciated.任何帮助表示赞赏。

Where is this script located in?这个脚本位于哪里? if its not in somewhere runs every time the player dies (for example StarterPlayer.StarterCharacterScripts ) your code wont work since the humanoid object attached to human variable no longer exists.如果每次玩家死亡时它不在某个地方运行(例如StarterPlayer.StarterCharacterScripts ),您的代码将无法工作,因为附加到human变量的humanoid object 不再存在。 (and you would get the attempt to index nil with 'MoveDirection' error) or if its already in an place where the scripts in it runs everytime player dies try using:WaitForChild() instead of:FindFirstChild() at line 4 (并且你会attempt to index nil with 'MoveDirection' )或者如果它已经在其中的脚本每次玩家死亡时运行的地方尝试使用:WaitForChild()而不是:第4行的:FindFirstChild()

use this:用这个:

local human = character:WaitForChild("Humanoid")本地人 = 字符:WaitForChild("Humanoid")

instead of this:而不是这个:

local human = character:FindFirstChild("Humanoid") local human = character:FindFirstChild("Humanoid")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM