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

I need help, my script is not working.
изображение_2022-02-20_132036

local hum = script.Parent.Humanoid
local anim2 = hum:LoadAnimation(script.Parent.dance1) --animation 1
local anim = hum:LoadAnimation(script.Parent.dance2) --animation 2

local anims = (math.random(3))

if anims == 1 then
	anim:Play()
	anim:AdjustSpeed(2) --speed
	wait(5)
end
if anims == 2 then
	anim2:Play()
	anim2:AdjustSpeed(2) --speed
	wait(5)
end
1 Like

I saw something wrong with your script. This will works, probably.

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 anims = (math.random(1,2)) --You need 2 numbers, first number is min and second number is max

if anims == 1 then
	anim:Play()
	anim:AdjustSpeed(2) --speed
	wait(5)
elseif anims == 2 then
	anim2:Play()
	anim2:AdjustSpeed(2) --speed
	wait(5)
end
1 Like

It is not working, only 1 animation is playing.

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 anims = (math.random(1,2)) --You need 2 numbers, first number is min and second number is max

if anims == 1 then
	anim:Play()
	anim:AdjustSpeed(2) --speed
	wait(5)
elseif anims == 2 then
	anim2:Play()
	anim2:AdjustSpeed(2) --speed
	wait(5)
end
1 Like

It’s because you don’t have a loop. You might need a loop to make this working

local anims = (math.random(1,2)) --This only choose 1 numbers between 1 and 2 for once

So if you want it to make more than 1 animations
Do

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 anims = (math.random(1,2)) --You need 2 numbers, first number is min and second number is max

Working = true --if this become false, the NPC will stop playing animations

while Working == true do
if anims == 1 then
	anim:Play()
	anim:AdjustSpeed(2) --speed
	wait(5)
elseif anims == 2 then
	anim2:Play()
	anim2:AdjustSpeed(2) --speed
	wait(5)
    end
end
1 Like

Still not working, npc is playing only 1 animation
изображение_2022-02-20_155624

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 anims = (math.random(1,2)) --You need 2 numbers, first number is min and second number is max

Working = true --if this become false, the NPC will stop playing animations

while Working == true do
	if anims == 1 then
		anim:Play()
		anim:AdjustSpeed(2) --speed
		wait(6)
	elseif anims == 2 then
		anim2:Play()
		anim2:AdjustSpeed(2) --speed
		wait(6)
	end
end
1 Like

wait sorry, it must be like 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

Working = true

while Working == true do
local anims = (math.random(1,2)) --You need 2 numbers, first number is min and second number is max
	if anims == 1 then
		anim:Play()
		anim:AdjustSpeed(2) --speed
		wait(6)
	elseif anims == 2 then
		anim2:Play()
		anim2:AdjustSpeed(2) --speed
		wait(6)
	end
end
3 Likes

Now it is playing different animation, not randomized animations.

Oh. i forgor :skull:

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

Working = true

while Working == true do
wait(6)
local anims = (math.random(1,2)) --You need 2 numbers, first number is min and second number is max
	if anims == 1 then
		anim:Play()
		anim:AdjustSpeed(2) --speed
	elseif anims == 2 then
		anim2:Play()
		anim2:AdjustSpeed(2) --speed
	end
end
1 Like

Try the following.

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

while true do
	local chosenanim = Anims[math.random(1,#Anims)]
    chosenanim.Priority = Enum.AnimationPriority.Action
	chosenanim:AdjustSpeed(2)
    chosenanim:Play()
	task.wait(5)
end
1 Like

This script is not working, animation doesn’t even play

oops, forgot to even make it play lol, check the new edit of the code

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.