簡體   English   中英

“嘗試使用‘MoveDirection’索引 nil”

[英]"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)

我已經嘗試更改變量並仔細檢查拼寫,但仍然返回一條錯誤消息。

任何幫助表示贊賞。

這個腳本位於哪里? 如果每次玩家死亡時它不在某個地方運行(例如StarterPlayer.StarterCharacterScripts ),您的代碼將無法工作,因為附加到human變量的humanoid object 不再存在。 (並且你會attempt to index nil with 'MoveDirection' )或者如果它已經在其中的腳本每次玩家死亡時運行的地方嘗試使用:WaitForChild()而不是:第4行的:FindFirstChild()

用這個:

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

而不是這個:

local human = character:FindFirstChild("Humanoid")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM