Where to add wait function?

Hi, I’m just wondering where to place my wait(5) function to prevent these animations from being spammed. Can anyone help me with that?

Script:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		wait(3)
		local siu = Instance.new("Animation", player.Character)
		local cheer = Instance.new("Animation", player.Character)
		local slide = Instance.new("Animation", player.Character)

		siu.AnimationId = "rbxassetid://12228840392"
		cheer.AnimationId = "rbxassetid://507770677"
		slide.AnimationId = "rbxassetid://12275574699"
		
		local siuanim = player.Character:FindFirstChild("Humanoid"):LoadAnimation(siu)
		local cheeranim = player.Character:FindFirstChild("Humanoid"):LoadAnimation(cheer)
		local slideanim = player.Character:FindFirstChild("Humanoid"):LoadAnimation(slide)


		game.ReplicatedStorage.Events.SiuFired.OnServerEvent:Connect(function(player)
			siuanim.Priority = Enum.AnimationPriority.Action4
			siuanim:Play()
		end)

		game.ReplicatedStorage.Events.CheerFired.OnServerEvent:Connect(function(player)
			cheeranim.Priority = Enum.AnimationPriority.Action4
			cheeranim:Play()
		end)
		game.ReplicatedStorage.Slide.OnServerEvent:Connect(function(player)
			slideanim.Priority = Enum.AnimationPriority.Action4
			slideanim:Play()
		end)
	end)
end)

No amount of waits is going to help if you don’t either disconnect the functions or add some logic to prevent them from playing the animation. Even if a function is waiting, the events that called the function can fire again.

Try

local isAnimationPlaying = false
...
        game.ReplicatedStorage.Events.SiuFired.OnServerEvent:Connect(function(player)
            if isAnimationPlaying then return end
            isAnimationPlaying = true
            
			siuanim.Priority = Enum.AnimationPriority.Action4
			siuanim:Play()
            siuAnim.Ended:Wait() --Same-ish as wait(anim.Length)
            
            isAnimationPlaying = false
		end)
2 Likes

Thanks, it works.

I have another small issue with animations, when I move the character itself in an animation, I want it to stay there, now it just goes back to the original position before you activated the animation.