:LoadAnimation() only plays once per animation

I’m trying to create a statue with dancing animations that are collected from a table in order, however, it’ll only play each animation once - stopping at the last one, though it prints a different animationId.

The animations used are default roblox dances, R6 versions.

local anim = script.Dance --animation instance

local anims = {
	"http://www.roblox.com/asset/?id=182435998", --default dances
	"http://www.roblox.com/asset/?id=182436842",
	"http://www.roblox.com/asset/?id=182436935"
}

local animator = Instance.new("Animator")
animator.Parent = hum

anim.AnimationId = anims[int]
local track = animator:LoadAnimation(anim)
track:Play()

it could be because of Roblox’s animations since they loop, and our action animations work (not necessary here), but how could I prevent this?

Int is the integer to get around the table.

1 Like

You must use a loop to play every animation and yield until the animation completes using track.Stopped:Wait()

I also recommend reading this AnimationTrack | Roblox Creator Documentation

1 Like

The issues are:

  • the dances are looped, so I don’t think .Looped would help in this scenario,
  • the animations get switched once a .ChildAdded was fired.

I don’t think yielding with track.Stopped:Wait() would help in this case. Sorry for the vague information.

I mean using a for loop

local humanoid = npc.Humanoid
local animationInstance = Instance.new("Animation", script)
local animations = {
  ["testidhere"] = 5 -- The number here it's how much times the thing will repeat
}

for anim, length in pairs(animations) do -- The index must be a string
     animationInstance.AnimationId = "rbxassetid://"..tostring(anim)
     local track = humanoid:LoadAnimation(animationInstance)
     track:Play()
     wait((track.Length * length))
     track:Stop()
     track:Destroy()
end

I’m trying to import this into a .ChildAdded event, not a for loop, therefore I won’t be able to know the length. The animations are default R6 dances, used as a general command /e. Here’s what I mean:

list.ChildAdded:Connect(function(child) --event
	local desc = plrs:GetHumanoidDescriptionFromUserId(userId)
	hum:ApplyDescription(desc)
	int += 1

	ui.Username.Text = name
    --animation stuff here
	anim.AnimationId = anims[int]
	local track = animator:LoadAnimation(anim)
	track:Play()
		
	if int == #anims then int = 0 end
end)

Once every single animation has been played, the table loop still continues, but the last ID in the table doesn’t stop playing at all when refreshing to the beginning, [1].

In other words:
when a child is added to a list, the character’s appearance is loaded in, the name for the text, and the animation is expected to change every time a child has been added, in int order. However, when it reaches the last string (or animation id), it continues playing it - not allowing the table to restart from beginning, but it prints the ids in the rightful order, just doesn’t load it in.

Okay, this was fixed by replacing a new Animator object every time a new animation needed to be played, and destroy the old one beforehand. Thank you for suggesting a solution, @gato_todomenso! Could use that in near future.