Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Attempt to index nil with "LoadAnimation"?

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

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

2 comments

0

I get an error saying, attempted to index nil with "WaitForChild", can you help?

0

Let me edit the answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.