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

Attempt to index nil with "LoadAnimation"?

Asked by 4 years ago

Hi! I'm trying to make an animation script for my sword, so that it may have two slash animations, I get the error in the title when I load the game, can anyone help?

By the way, I'm using a LocalScript.

game.Players.PlayerAdded:connect(function(player)
    humanoid = player.Character:WaitForChild("Humanoid")
end)

wait(5)

local slashType = false
slashA = humanoid:LoadAnimation(script.Parent.Slash)
slashB = humanoid:LoadAnimation(script.Parent.SlashB)

if slashType == false then
    script.Parent.Activated:connect(function()
        slashA:Play()
        slashType = true
    end)
end

if slashType == true then
    slashB:Play()
    slashType = false
end

script.Parent.Unequipped:connect(function()
    slashA:Stop()
    slashB:Stop()
end)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

In lines 08 and 09, humanoid doesn't exist as it's contained in its own block in line 1. Also, since this is a local script you can simply wait for the humanoid of the local player (hint: local scripts don't run unless they're in the player or character).

Edit: Try waiting for the player to load before you find the character.

wait(5) -- the wait(5) is just an example, you can just wait for the player.CharacterAdded event and nest all of your functions in it.
local humanoid = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid")

-- rest of the code
0
I get an error saying, attempted to index nil with "WaitForChild", can you help? Diyamund 0 — 4y
0
Let me edit the answer. radiant_Light203 1166 — 4y
Ad

Answer this question