If statement ignored in a serverscript

Hello! I’m currently trying to make a viewportframe showing a model (character model) playing a random animation.
This is the serverscript:

while true do
	print("start")
	if math.random(1,4) == 1 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a1)
		anim:Play()
		print("1")
	elseif math.random(1,4) == 2 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a2)
		anim:Play()
		print("2")
	elseif math.random(1,4) == 3 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a3)
		anim:Play()
		print("3")
	elseif math.random(1,4) == 4 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a4)
		anim:Play()
		print("4")
	end
	print("done")
	wait(60)
	for i,v in pairs(script.Parent.Humanoid.Animator:GetPlayingAnimationTracks()) do
		v:Stop()
	end
	if math.random(1,4) == 1 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a1)
		anim:Play()
		print("1")
	elseif math.random(1,4) == 2 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a2)
		anim:Play()
		print("2")
	elseif math.random(1,4) == 3 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a3)
		anim:Play()
		print("3")
	elseif math.random(1,4) == 4 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a4)
		anim:Play()
		print("4")
	end
	wait(60)
end

This is where the script is located:


Any help would be appreciated!

You should really research on how to write compact code, instead of your

	if math.random(1,4) == 1 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a1)
		anim:Play()
		print("1")
	elseif math.random(1,4) == 2 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a2)
		anim:Play()
		print("2")
	elseif math.random(1,4) == 3 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a3)
		anim:Play()
		print("3")
	elseif math.random(1,4) == 4 then
		local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.a4)
		anim:Play()
		print("4")
	end

you can just write

local anim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent["a"..tostring(math.random(1,4))])
anim:Play()

Do this and your problem of the statement being ignored will be solved too

(By the way, the reason for why your statement was ignored is because in your huge if-else statement a new number was being rolled for each check, so if you were unlucky you could have went through the entire if-else statement without activating any of its checks)

2 Likes

that still didn’t work
the character still has no animations playing
image
and i did do your method of compact code

while true do
	print("start")
	script.Parent.Humanoid.Animator:LoadAnimation(script.Parent["a"..tostring(math.random(1,4))]):Play()	
	print("done")
	wait(20)
	for i,v in pairs(script.Parent.Humanoid.Animator:GetPlayingAnimationTracks()) do
		v:Stop()
	end
	script.Parent.Humanoid.Animator:LoadAnimation(script.Parent["a"..tostring(math.random(1,4))]):Play()
	wait(20)
end

any thoughts?

Does it print out any errors?
If not, perhaps try checking if you are reffering to the correct Humanoid.

animations don’t play in viewport frames

Completely forgot about that.
Look into WorldModels

1 Like