How do you make animations play in order?

Hey devs, I’ve recently created a few tools that play animations randomly, it there any type of way to make it play in order?
It looks really weird playing randomly because sometimes it actually does the same animations back to back.

  1. What do you want to achieve?
    I’m trying to create/find a script that actually plays animations in order.

  2. What solutions have you tried so far?
    I’ve surfed the DevForum, and I’ve found solutions but it doesn’t work for my tool specifically.

Here is the script that I currently use and it works perfectly.

local debounce = false
script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		if debounce == false then
			local attack = math.random(1,5)
			if attack == 1 then
				animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animations.Punch1)
			end
			if attack == 2 then
				animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animations.Punch2)
			end
			if attack == 3 then
				animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animations.Punch3)
			end
			if attack == 5 then
				animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animations.Punch5)
			end
			
			animation:Play()
			debounce = true
			wait(0.11) -- cooldown
			debounce = false
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	animation:Stop()
end)

^
This script is a localscript and is a child for a tool.
This is where the animations are taken from:
vvvvv
image

1 Like

Well, you have a math.random() function to currently get your next animation, try having a global value that you increase each time.

1 Like

local debounce = false
local current_animation_number = 1 -- Animations will Start from the Number 1
local animations_folder = script.Parent.Animations

local animation
script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		if debounce == false then
			animation=game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animations_folder[tostring(current_animation_number)])
			animation:Play()
			debounce = true
			wait(0.11) -- cooldown
			debounce = false
			if current_animation_number < #animations_folder:GetChildren() then
				current_animation_number=current_animation_number+1
			else
				current_animation_number=1
			end
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	if animation~=nil then
		animation:Stop()
	end
end)


Every time the Player has Clicked with their tool,
We Increase A Global Variables Number With +1

Now when the player clicks the tool, we just check what the global variable is,
find the animation instance with the variable name, and play it.

( i know im not the best explainer )

FOR THIS TO WORK, NAME YOUR ANIMATIONS IN ORDER
Example,
Name the first Animation 1
Name the second animation 2

etc.

1 Like

It definitely works, but it works the first time and just stops working after on.

1 Like

I edited the same script again,
check it out again.

I just added an if statement to see if the max animations you have is reached or not,
if reached then we restart the count.

1 Like

It works, I appreciate your help.

1 Like