Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would i add a debounce to this script?

Asked by 4 years ago

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

Log in to vote
1
Answered by
Elixcore 1337 Moderation Voter
4 years ago
Edited 4 years ago
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)
Ad

Answer this question