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

How to make Animation GUI start and continue when clicked, then stops animation when clicked again?

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.

1 answer

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.

6 comments

0

Script that stops the dance? There is no script that stops it. The animation just plays then ends. Or do you mean the script that puts you back to normal, like line 15-19 on the first script I showed?

0

Hi so, it works but I still don't know what you mean by the script that stops the dance. When I click the button the animation works and it stays but then I can't go back to normal because I don't know what stops the animation.

0

The part where it makes you go back to normal, sets your speed to normal and :Stop() the animations.

0

The script that puts you back to normal.

0

so this (line 16-19 in the first script I showed) >> wait(CoolDown + dance.Length) hum.JumpPower = 50 hum.WalkSpeed = 16 CanEmote = true

0

Yes.

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