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

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

Asked by 3 years ago

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)
0
Maybe try character:WaitForChild("Humanoid") ? Spjureeedd 385 — 3y
0
ok thx kidsteve923 139 — 3y
0
YES OMG IT WORKS TYT kidsteve923 139 — 3y
0
lol don't underestimate WaitForChild Spjureeedd 385 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

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)
Ad

Answer this question