Help with making a animation play when a button is clicked

Hi! I want to make a button that when you click it plays an animation! Im having issues with it. The script doesn’t seem like its working sadly. I don’t know what the issue is. I think that the issue is within the function call. I hope you can help me!

local animation = script:WaitForChild("Animation")
local button = script.Parent

local debounce = 1

button.MouseButton1Click:Connect(function()
    animation:play()
    wait(debounce)
end)

There’s actually a function that we use to play animations that’s called: LoadAnimation, but it depends on where your Character hierarchy is & where your script is for the script to load the animation

Also, is this a Local or Server script?

1 Like

local. Its a local script. It seems like the right one to use.

1 Like

Correct you are! Using a LocalScript, we can actually get the Character fairly easy since it’s referenced from the client side! So we can get the player & add a bit more to our code:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Button = script.Parent
local Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator")
local Animation = script:WaitForChild("Animation")
local Debounce = false

Button.MouseButton1Click:Connect(function()
    if Debounce == false then
        Debounce = true

        local LoadAnimation = Animator:LoadAnimation("Animation")
        LoadAnimation:Play()
        wait(1)
        Debounce = false
    end
end)
1 Like

I am afraid this still doesn’t work.

Really? Are you getting any errors from your Output at all? You should either see them in Yellow or Red

no. Im not. Im using an R6 animation and my game setting says the characters are R6

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Button = script.Parent
local Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator")
local Animation = script:WaitForChild("Animation")
local Debounce = false
print("Script online")

Button.MouseButton1Click:Connect(function()
    print("Button fired")
    if Debounce == false then
        print("Playing animation")
        Debounce = true

        local LoadAnimation = Animator:LoadAnimation(Animation)
        LoadAnimation:Play()
        wait(1)
        Debounce = false
        print("Animation ended?")
    end
end)

Hm weird, try this and see what prints out?

1 Like

the output sent some stuff I don’t understand.

Hm, chances are either your Animation variable or the LoadAnimation variable isn’t valid

Can you double check to see what Animation is supposed to be? I’m assuming an Animation object?

um. Animation is not an object. Its simply an emote. I classified it as a idle animation using th roblox animator.

Screen Shot 2021-04-06 at 5.23.11 PM

Wait I’m dumb LOL I defined the LoadAnimation variable as a string

Ok try the script again, I edited it & it should be fixed by now

thank you! It worked! I appreciate it a lot!

1 Like

Excuse my dumb mistakes aha Anytime! Do keep in mind that you can use the LoadAnimation function when you want to play a specific animation when scripting :wink:

thanks! I will look more into it! I have been studying animations lately. Im soon going to go into boolean values after I understand how to script animations! Thanks again!

Some times it does not work the AP is not fixed right

What does AP mean? Sorry I don’t know much about scripting as you know.