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

How would i add a debounce to this script?

I have recently been making a game, were you need to click a button to make an animation play however, I am having big trouble with doing so that the animation only plays once. How would I fix it? I have tried to add a wait() function in the end, however it doesn´t help on anything, the animation will still play multiple times if the player clicks the button again.

local hum = game.Workspace.Dannebrog.HumanoidCharacter
local set = hum.Parent.Settings
local sp = set.Speed
local enabled = set.Enabled
local ClickDetector = game.Workspace.FlagSign.Sign.ClickDetector

local humanim = hum:LoadAnimation(hum.Parent.Settings.IdleAnimation)

ClickDetector.MouseClick:Connect(function()
	humanim:Play()
	humanim.Looped = false
	humanim:AdjustSpeed(sp.Value)
end)

1 answer

✓ Accepted answer
local hum = workspace.Dannebrog.HumanoidCharacter
local set = hum.Parent.Settings
local sp = set.Speed
local enabled = set.Enabled
local ClickDetector = game.Workspace.FlagSign.Sign.ClickDetector

local humanim = hum:LoadAnimation(hum.Parent.Settings.IdleAnimation)
humanim.Looped = false -- placed before the event


ClickDetector.MouseClick:Connect(function(player)
	if not humanim.IsPlaying then -- debounce, checking if animation is not playing, then executing code.
		 humanim:AdjustSpeed(sp.Value) -- only place this here if sp.Value changes, if not place it before the event.
  		 humanim:Play()
	end
end)

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