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

Animation Cycle keeps looping first animation?

Im trying to make a sword tool with an animation cycle so on the first hit it will play an animation, then on the second hit a different, so on, and so forth. But for some reason whenever you click just once it keeps looping the first animation over and over and doesnt stop. The script is a local script inside of the tool

local Anim1 = script.Parent.Animations.Swing2
local Anim2 = script.Parent.Animations.Swing1
local Anim3 = script.Parent.Animations.Swing3

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Hum = Char:WaitForChild("Humanoid")

local Swing1 = Hum.Animator:LoadAnimation(Anim1)
local Swing2 = Hum.Animator:LoadAnimation(Anim2)
local Swing3 = Hum.Animator:LoadAnimation(Anim3)

local AnimationTable = {
	Swing1,
	Swing2,
	Swing3
}

local CurrentSwing = AnimationTable[1]

local function Swing ()
	for i, v in pairs(AnimationTable) do
		if v == CurrentSwing then
			v:Play()
			
			v.Ended:Connect(function()
				CurrentSwing = AnimationTable[i + 1]
			end)
		end
	end
end

script.Parent.Activated:Connect(function()
	Swing()
end)

1 comment

0

Check if the looping is on in the animation edit thing

1 answer

To turn off the looping, you can set AnimationTrack.Looped to false.

local Anim1 = script.Parent:WaitForChild("Animations").Swing2
local Anim2 = script.Parent:WaitForChild("Animations").Swing1
local Anim3 = script.Parent:WaitForChild("Animations").Swing3

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Hum = Char:WaitForChild("Humanoid")
local Animator = Hum:WaitForChild("Animator")

local Swing1 = Animator:LoadAnimation(Anim1)
local Swing2 = Animator:LoadAnimation(Anim2)
local Swing3 = Animator:LoadAnimation(Anim3)

local AnimationTable = {
	[1] = Swing1,
	[2] = Swing2,
	[3] = Swing3
}

local CurrentSwing = AnimationTable[1]

local function Swing()
	for i, v in ipairs(AnimationTable) do
		if v == CurrentSwing then
			v.Looped = false -- turns off the looping
			v.Priority = Enum.AnimationPriority.Action4
			v:Play()
			
			v.Ended:Connect(function()
				CurrentSwing = AnimationTable[i + 1]
				if CurrentSwing > #AnimationTable then -- if the last animation is finished
					CurrentSwing = 1 -- goes back to the first animation
				end
			end)
		end
	end
end

script.Parent.Activated:Connect(Swing)

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