Issue with Debounce and Spam-Clicking

Hello. I am attempting to write code that processes animations whereby a Server script sends an event to the player. This event is processed by a LocalScript that plays the animation.

I am playing animations in series and I pass this information as a table. The final animation of this table is always a looped animation. In order to prevent animations from getting jumbled up, I introduced a debounce. When the debounce is true, new series of animations cannot be played. The debounce is set to false when the final, looped animation is reached.

Here is my code:

local debounce = script.Parent.IsAnimating
local function playAnimationSequence(animationData)
	if debounce.Value then
		return
	end
	
	debounce.Value = true
	
    stopAllAnimations()

    --
	for i, animInfo in ipairs(animationData) do
        local animTrack = playAnimation(animInfo.Name, animInfo.Speed or 1)
        if animTrack then
			if animInfo.Looped then
				debounce.Value = false
				-- Loop the animation indefinitely
				return
            else
				-- Non-looped animations need to wait until they finish
				task.wait(animTrack.Length / math.abs(animInfo.Speed or 1))  -- Wait for the duration of the animation
            end
        end
	end
end

This is how I am handling animations on the server side:

elseif input == Enum.UserInputType.MouseButton1 and isbegan and equippingtool and player:GetAttribute("isLoaded") and not isAiming and not isTrailing and not isOrdering and not Skirmish and not debounce.Value then
	wait(0.25)
		MakeReady = not MakeReady

		if MakeReady then
			MusketAnimationEvent:FireClient(player, {
				{Name = "MakeReady", Speed = 1.5},         
				{Name = "MakeReadyIdle", Speed = 1, Looped = true},       
			})
		else
			MusketAnimationEvent:FireClient(player, {
				{Name = "MakeReady", Speed = -1.5},         
				{Name = "Default", Looped = true},       
			})			
		end

The main issue I’m running into is whenever I spam click, it seems to skip over animations. So the animations do not begin at the logical, last position. I have included a video outlining this:

Medal.tv Video Link

1 Like

I would think its because if everytime you click with mouse it tries to run playAnimation sequence function, and this would be stoppping / cancelling the previous animations and restarting from the begging.

what does it look like if you click once and let the loop animation play?

So I ended up fixing my issue. What I did was introduce a debounce by setting an attribute to the player “IsAnimating.” This seemed to resolve the issue globally as now all scripts that play an animation cannot do so until the player’s attribute is set to false. I also rewrote the logic for my code so that “IsAnimating” would be set to false when a looped animation is played. Thanks tho!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.