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