How do i make a script that makes npc play random animation?

I dont need to make it play forever, it should change randomly after animation stopped playing.

--Get the anims
local Humanoid = script.Parent.Humanoid
local Anims = {
	Humanoid:LoadAnimation(script.Parent.dance1),
	Humanoid:LoadAnimation(script.Parent.dance2)
}

--Configure the anins
for i,v in pairs(Anims) do
	v.Priority = Enum.AnimationPriority.Action
	v:AdjustSpeed(2)
end

--Play the anims
while true do
	local chosenanim = Anims[math.random(1,#Anims)]
	chosenanim:Play()
	print("Played new animation")
	
	if chosenanim.Looped then
		chosenanim.DidLoop:Wait()
		chosenanim:Stop()
		print("Stopped animation")
	else
		chosenanim.Stopped:Wait()
		print("Animation finished")
	end
end

For some reason animation is playing forever, not changing

Can you show your output, what did it print?


First line, dont look at “infinite yield possible” it is other script

--Get the anims
local Humanoid = script.Parent.Humanoid
local Anims = {
	Humanoid:LoadAnimation(script.Parent.dance1),
	Humanoid:LoadAnimation(script.Parent.dance2)
}

--Configure the anins
for i,v in pairs(Anims) do
	v.Priority = Enum.AnimationPriority.Action
	v:AdjustSpeed(2)
end

--Play the anims
while true do
	local chosenanim = Anims[math.random(1,#Anims)]
	chosenanim:Play()
	print("Played new animation")

	if chosenanim.Looped then
		print("The animation loops, waiting for it to loop")
		chosenanim.DidLoop:Wait()
		chosenanim:Stop()
		print("Stopped animation")
	else
		print("Waiting for animation to stop")
		chosenanim.Stopped:Wait()
		print("Animation finished")
	end
end


Selected animation loops forever, i think this is happening because of this

I agree that we both taking ages to fix your problems :joy:
Try this.

local hum = script.Parent.Humanoid
local anim2 = hum:LoadAnimation(script.Parent.dance1) --animation 1
anim2.Priority = Enum.AnimationPriority.Action
local anim = hum:LoadAnimation(script.Parent.dance2) --animation 2
anim.Priority = Enum.AnimationPriority.Action --This will forced this animation to play

local working = true

while working == true do
	wait(6)
	local choose = math.random(1,2)/1
	if choose == 1 then
		anim:Play()
		anim:AdjustSpeed(2)
		anim.Stopped:Wait()
	end
	if choose == 2 then
		anim2:Play()
		anim2:AdjustSpeed(2)
		anim2.Stopped:Wait()
	end
end



I will try to change hum to humanoid and see if it works
I tried, not working

The /1 is useless since any number you get, and if you divide it by 1, you’ll get the same number.

Again, I don’t wanna read the entire thing, so can anybody explain shortly what he wants to achieve?

Here
I need to make it work like this: randomized animation starts playing (for example dance1) and after animation stopped, script randomly selects which animation will play now.

It’s not.
The /1 actually is range for math.Random

For some reason the Stopped event wont fire, try this then

--Get the anims
local Humanoid = script.Parent.Humanoid
local Anims = {
	Humanoid:LoadAnimation(script.Parent.dance1),
	Humanoid:LoadAnimation(script.Parent.dance2)
}

--Configure the anins
for i,v in pairs(Anims) do
	v.Priority = Enum.AnimationPriority.Action
	v:AdjustSpeed(2)
end

--Play the anims
while true do
	local chosenanim = Anims[math.random(1,#Anims)]
	chosenanim:Play()
	print("Played new animation")
	
	task.wait(chosenanim.Length)
	print("Anim finished")
end

image
Output shows that script is working, but only 1 animation is playing

I forgot to put local hum = script.Parent.Humanoid, check the code once again

Then try this maybe?

local hum = script.Parent.Humanoid

local animations = {
    [1] = hum:LoadAnimation(script.Parent:WaitForChild("dance1")); --animation 1
    [2] = hum:LoadAnimation(script.Parent:WaitForChild("dance2")); --animation 2
};

while task.wait(5) do
    local rand = math.random(2) -- automatically sets its range from 1 to 2
    local chosen = animations[rand]

    chosen:Play()
    chosen:AdjustSpeed(2)
    
    chosen.Stopped:Connect(function()
        print("Ended")
    end) -- You can add more or just remove it
end

Not working, animation plays forever

--Get the anims
local Humanoid = script.Parent.Humanoid
local Anims = {
	Humanoid:LoadAnimation(script.Parent.dance1),
	Humanoid:LoadAnimation(script.Parent.dance2)
}

--Configure the anins
for i,v in pairs(Anims) do
	v.Priority = Enum.AnimationPriority.Action
	v:AdjustSpeed(2)
end

--Play the anims
local PastAnim = nil
while true do
	print("There is " .. #Anims .. "to choose from.")
	local chosenanim = Anims[math.random(1,#Anims)]
	
	--Stop duplicates
	if chosenanim == PastAnim then
		repeat
			chosenanim = Anims[math.random(1,#Anims)]
		until chosenanim ~= PastAnim
	end
	
	PastAnim = chosenanim
	chosenanim:Play()
	print("Played new animation")
	
	task.wait(chosenanim.Length)
	print("Anim finished")
end

Your script is not working too, same problem: animation plays forever

Is your animation’s looped property set to true?