Using math.random to handle image properties

I have been coding a script that uses math.random to make UIs visible and invisible, I have a problem with this code and it doesn’t show me an error in the output. It prints normally but it doesn’t set the UIs properties.

while true do
		local chance = math.random(1,100) 
		local races = game:GetService("StarterGui").test["Spin UI"]
	
		if chance <= 1 then -- If it falls between 1 and 1 [LSSJ]
		print("There is a 1% chance of this occuring [LSSJ]")
		
		races["LSSJ Race"].Visible = true
		wait(0.1)
		races["LSSJ Race"].Visible = false
else
		if chance <= 5 then -- If it falls between 1 and 5 [Acrosian]
		print("There is a 5% chance of this occuring [Acrosian]")
			
		races["Acrosian Race"].Visible = true
		wait(0.1)
		races["Acrosian Race"].Visible = false
else
		if chance <= 5 then -- If it falls between 1 and 5 [Majin]
		print("There is a 5% chance of this occuring [Majin]")

else
		if chance <= 7 then -- If it falls between 1 and 7 [Tuffle]
		print("There is a 7% chance of this occuring [Tuffle]")
					
		races["Tuffle Race"].Visible = true
		wait(0.1)
		races["Tuffle Race"].Visible = false
else
		if chance <= 10 then -- If it falls between 1 and 10 [Sphinx]
		print("There is a 10% chance of this occuring [Sphinx]")
						
		races["Sphinx Race"].Visible = true
		wait(0.1)
		races["Sphinx Race"].Visible = false
else
		if chance <= 10 then -- If it falls between 1 and 10 [Jinago]
		print("There is a 10% chance of this occuring [Jinago]")
							
		races["Jinago Race"].Visible = true
		wait(0.1)
		races["Jinago Race"].Visible = false
else
		if chance <= 20 then -- If it falls between 1 and 20 [Saiyan]
		print("There is a 20% chance of this occuring [Saiyan]")
								
		races["Saiyan Race"].Visible = true
		wait(0.1)
		races["Saiyan Race"].Visible = false
else
		if chance <= 20 then -- If it falls between 1 and 20 [Namekian]
		print("There is a 20% chance of this occuring [Namekian]")
									
		races["Namekian Race"].Visible = true
		wait(0.1)
		races["Namekian Race"].Visible = false
else
		if chance <= 22 then -- If it falls between 1 and 22 [Human]
		print("There is a 22% chance of this occuring [Human]")
										
		races["Human Race"].Visible = true
		wait(0.1)
		races["Human Race"].Visible = false
						
end		
end
end
end
end		
end
end
end
end
end

If there is also a more efficient way to do this script then please let me know

2 Likes

hey dino,
your script works, but it sets the properties of the frames inside game.StarterGui (therefore updating StarterGui wont change what the player currently sees on their screen because it uses PlayerGui not StarterGui),
StarterGui is what players get when they join the game, you need to change player.PlayerGui instead.

If there is also a more efficient way to do this script then please let me know

Instead of using

if chance==1 then
print("a")
else
		if chance <= 5 then
print("b")
end
end 

You can use

if chance==1 then
print("a")
elseif chance<=5
print("b")
end

Hope this helps!

2 Likes