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

I have a problem, here is the video


Animation doesn’t change

Animation is playing, but it is not randomized

It is randomized, however I did not bother to implement a check to stop anims from playing twice, in theory its possible for the same anim to keep playing forever.

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.

--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()
	chosenanim.Stopped:Wait()
end

Did you mean that the hacker guy in the black suit aren’t playing the animation you wanted?

The same problem again, script randomly selects an animation but it starts to play forever

Do the animations loop? characters

No, i need to make it work like this:
Script chooses random animation,- animation dance1 playing - when animation is stopped - repeat the 1 step again.

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.