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

Did you checked that you accidentally looped the animation in the animation Properties?

ah, yes same reply

don’t click it’s off-topic


Same dance playing forever

image
Where is it?

Try @wf_sh’s (sorry for the ping) code except add this line in the for loop

v.Looped = false

and, yes im lazy to even copy-paste the code :sloth:

--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.Looped = false
	chosenanim:Play()
	print("Played new animation")

	task.wait(chosenanim.Length)
	print("Anim finished")
end

I just borrow the script from @wf_sh

Could’ve just put the looped = false line in for loop but ok

I dont know why but animation is playing forever

ok, ok in the for loop just add a print saying print(v.Looped)

Same animation

--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.Looped = false
	chosenanim:Play()
	print("v.looped")
	print("Played new animation")

	task.wait(chosenanim.Length)
	print("Anim finished")
end
--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)
    print("IsLooped: " .. v.Looped)
    v.Looped = false
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.Looped = false
	chosenanim:Play()
	print("Played new animation")

	task.wait(chosenanim.Length)
	print("Anim finished")
end

Output shows an error

--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)
    print("IsLooped: " .. tostring(v.Looped))
    v.Looped = false
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.Looped = false
	chosenanim:Play()
	print("Played new animation")

	task.wait(chosenanim.Length)
	print("Anim finished")
end

how many times do I have to copy-paste :skull:

It still loops 1 animation forever but thank you for your help.
I will just make npc play 1 animation.

Aw man, sorry man we couldn’t fix your problem. But I really hope your game is amazing, good luck!

1 Like

No problem, thank you and good luck in developing

1 Like
local npc = script.Parent
local humanoid = npc.Humanoid
local dance1 = npc.dance1
local dance2 = npc.dance2
local track1 = humanoid:LoadAnimation(dance1)
local track2 = humanoid:LoadAnimation(dance2)
repeat task.wait() until track1.Length > 0
repeat task.wait() until track2.Length > 0

while true do
	local random = math.random(2)
	if random == 1 then
		track1:Play()
		track1:AdjustSpeed(2)
	elseif random == 2 then
		track2:Play()
		track2:AdjustSpeed(2)
	end
	task.wait(5)
end

Disable the “.Looped” property for each animation instance.

1 Like