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

Why wont my animation play when I click? [PLEASE HELP]

The localscript:

local Tool = script.Parent
local Player = game:GetService("Players").LocalPlayer
local Animation = Tool.Animation

local canSwing = true

local Debounce = 1

script.Parent.Activated:Connect(function()
    local swingAnimation = Player.Character.Humanoid:LoadAnimation(Animation)
    if canSwing then
        canSwing = false
        swingAnimation:Play()
        wait(Debounce)
        canSwing = true

    end
end)

This local script is in the tool.

Just incase you wanted to try the animation to test it and see if it works, the animation id:

5360112825

6 comments

0

does it prints anything on console?

0

w A t ? if your wondering it doesnt print anything on output, no errors

0

w A t ? if your wondering it doesnt print anything on output, no errors

0

Why did you put wait{} instead of wait()

0

is it your animation?

0

bc is it isnt you cant use it, it has to be made by you

1 answer

Hello!

I have reveiewed your script, and the reason why the animation didn't work, is because the script runs before the Humanoid can be spawned, and so the script has no humanoid to refer to upon joining, which then gives you the error: attempt to index nil with 'Humanoid' An easy fix to this issue is to switch the line of where swingAnimation is defined,

Try this script:

local Tool = script.Parent
local Player = game:GetService("Players").LocalPlayer
local Animation = Tool.Animation

local canSwing = true

local Debounce = 1

script.Parent.Activated:Connect(function()
	local swingAnimation = Player.Character.Humanoid:LoadAnimation(Animation)
    if canSwing then
        canSwing = false
        swingAnimation:Play()
        wait(Debounce)
        canSwing = true

    end
end)

Hope this helped!

Edited:

wait()
local Tool = script.Parent
local Player = game:GetService("Players").LocalPlayer
local Animation = Tool.Animation

local canSwing = true

local Debounce = 1

script.Parent.Activated:Connect(function()
    local swingAnimation = Player.Character.Humanoid:LoadAnimation(Animation)
    if canSwing then
        canSwing = false
        swingAnimation:Play()
        wait(Debounce)
        canSwing = true

    end
end)

2 comments

0

n o w o r k

0

Apologies! Please try the edited version

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