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

=Players.kidsteve923.Backpack.Tool.LocalScript:10: attempt to index nil with 'Humanoid'?

I get a error when i run this please help me

local player = game:GetService("Players").LocalPlayer
local character = player.Character
local animation = script.Parent:FindFirstChild("Animation")
local swingAnimation

local canSwing = true

local debounce = 1

swingAnimation = character.Humanoid:LoadAnimation(animation)

script.Parent.Activated:Connect(function()
	if canSwing then
		canSwing = false
		swingAnimation:Play()
		wait(debounce)
		canSwing = true
	end
end)

4 comments

0

Maybe try character:WaitForChild("Humanoid") ?

0

ok thx

0

YES OMG IT WORKS TYT

0

lol don't underestimate WaitForChild

1 answer

✓ Accepted answer

Bro, Humanoid:LoadAnimation is deprecated. It shouldn't be put to work anymore. Instead, use Animator:LoadAnimation. I refactored your code a little bit. Just copy my script below, it should work just fine. But now, even more optimized. I hope this helps!

local player = game:GetService("Players").LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator") --this!!
local swingAnimation = animator:LoadAnimation(animation)

local animation = script.Parent:FindFirstChild("Animation")

local canSwing = true
local debounce = 1

script.Parent.Activated:Connect(function()
    if canSwing then
        canSwing = false

        swingAnimation:Play()

        wait(debounce)
        canSwing = true
    end
end)

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