Hello, I am trying to make a GUI button where if you clicked, an animation would play, but I want it to continue with the animation until if you were to click again, the animation would stop.
I have it to where you can click a button and the animation works but it only plays the animation then stops when the animation is done, and if you clicked the button again the animation will happen again, and so on. I got the script from a youtube tutorial. This is a LocalScript in a TextButton under ScreenGui.
local btn = script.Parent local plr = game.Players.LocalPlayer local char = plr.Character or plr.CharacterAdded:Wait() local hum = char:WaitForChild("Humanoid",10) local CanEmote = true local CoolDown = 0 btn.MouseButton1Click:connect(function() if CanEmote == true then hum.JumpPower = 0 -- this makes it so the player can't move while the animation plays hum.WalkSpeed = 0 CanEmote = false local dance = hum:LoadAnimation(script:FindFirstChild("Anim1")) dance:Play() wait(CoolDown + dance.Length) hum.JumpPower = 50 -- goes back to original, player can move normally again hum.WalkSpeed = 16 CanEmote = true end end)
In the local script there is an animation, which is named Anim1. ^^That is what I got from the video but I want the animation to continue until clicked again so I tried doing something in the middle but now the button won't work.VV
local btn = script.Parent local plr = game.Players.LocalPlayer local char = plr.Character or plr.CharacterAdded:Wait() local hum = char:WaitForChild("Humanoid",10) local CanEmote = true local CoolDown = 0 btn.MouseButton1Click:connect(function() if CanEmote == true then hum.JumpPower = 0 hum.WalkSpeed = 0 CanEmote = false local dance = hum:LoadAnimation(script:FindFirstChild("Anim1")) dance:Play() btn.MouseButton1Click:Connect(function() --this is what I added, assuming clicking again will stop the animation? if CanEmote == false then --I did the opposite hum.JumpPower = 50 hum.WalkSpeed = 16 CanEmote = true --opposite wait(CoolDown + dance.Length) hum.JumpPower = 50 hum.WalkSpeed = 16 CanEmote = true end end) end)
Does anyone know what needs to be added or changed to my script in order for my animation to continue until stopped? Beginner with scripting but I kind of know how to read scripts, so if you could also explain in detail that would be appreciated because I also want to understand what I'm getting wrong. Any help is appreciated, thank you.
Use a debounce.
local btn = script.Parent local plr = game.Players.LocalPlayer local char = plr.Character or plr.CharacterAdded:Wait() local hum = char:WaitForChild("Humanoid",10) local CanEmote = true local CoolDown = 0 local debounce = false -- Debounce btn.MouseButton1Down:Connect(function() if debounce == false then debounce = true if CanEmote == true then -- Dancing thingamajingy end end if debounce ~= false then -- Insert script which stops the dance debounce = false end end)
Make sure to use the order i used.