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

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

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

✓ Accepted answer

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!

1 comment

0

From there, you can then reference humanoid from the character by doing local humanoid = character:WaitForChild("Humanoid")

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