Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does it keep saying the Character value is nil when I try to use an animation?

Asked by
TtuNkK 37
4 years ago

I am trying to make an idle and attack animation for a sword, but when I try to get the animation to run on the humanoid, it won't work saying, "attempt to index field 'Character' (a nil value)." Someone, please assist me on how to fix this problem.

Here's the local script:

local CanAttack = true
local idle = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.idle)
local attack = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.attack)
script.Parent.Equipped:Connect(function()
    idle:Play()
    script.Parent.Activated:Connect(function()
        if CanAttack then
            idle:Stop()
            attack:Play()
            CanAttack = false
            wait(1)
            attack:Stop()
            idle:Play()
            CanAttack = true
            script.Parent.CanDamage.Value = true
        end
    end)
end)

1 answer

Log in to vote
0
Answered by 4 years ago

Try this:

local player = game.Players.LocalPlayer
local character = player.Character or Player.CharacterAdded:Wait()

Try using that as your character line at the top of your script.

Because for the character, it's looking for the character, and if the script can't find the character from the player it will wait until the character is added to workspace and reference that variable to the character.

If it works, let me know by taking this as an answer. If it doesn't comment below what happens and I'll try to fix it for you!

0
From there, you can then reference humanoid from the character by doing local humanoid = character:WaitForChild("Humanoid") killerbrenden 1537 — 4y
Ad

Answer this question