This property sets whether the animation will repeat after finishing. If it is changed while playing the result will take effect after the animation finishes.
The Looped property for
AnimationTrack defaults to how it was set in the animation editor. However this property can be changed, allowing control over the
AnimationTrack while the game is running. Looped also correctly handles animations played in reverse (negative
AnimationTrack/Speed). After the first keyframe is reached, it will restart at the last keyframe.
This property allows the developer to have a looping and non looping variant of the same animation, without needing to upload two versions to Roblox.
The animation in this example normally loops. After the player and the animation are loaded the animation is played in a non-looped fashion then in a looped fashion.
local Players = game:GetService("Players") local localPlayer = Players.LocalPlayer while not localPlayer.Character do wait() end local character = localPlayer.Character local humanoid = character:WaitForChild("Humanoid") local animation = Instance.new("Animation") animation.AnimationId = "rbxassetid://507770453" local animationTrack = humanoid:LoadAnimation(animation) animationTrack.Looped = false wait(3) animationTrack:Play() wait(4) animationTrack.Looped = true animationTrack:Play()
Play AnimationTrack for a Number of Loops
The function in this code sample will play an AnimationTrack on a loop, for a specific number of loops, before stopping the animation.
In some cases the developer may want to stop a looped animation after a certain number of loops have completed, rather than after a certain amount of time. This is where the DidLoop event can be used.
local function playForNumberLoops(animationTrack, number) animationTrack.Looped = true animationTrack:Play() local numberOfLoops = 0 local connection = nil connection = animationTrack.DidLoop:connect(function() numberOfLoops = numberOfLoops + 1 print("loop: ", numberOfLoops) if numberOfLoops >= number then animationTrack:Stop() connection:Disconnect() -- it's important to disconnect connections when they are no longer needed end end) end