Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to do i make it every time i click a part it randomly generated a number out of 3?

So I'm trying to do a loading scene where it has hints towards the game at the bottom and I have a randomizer script

local num = math.random(1,3) 
local BrickEvent = game.ReplicatedStorage.Events.Gui

BrickEvent.OnClientEvent:Connect(function(plr)
if num == 1 then
		  print("you got Critical Hit Fact")
		script.Parent.ScreenGui.Frame.HintCriticalDamage.Visible=true
 
 elseif num == 2 then
		 print("you got Stamina Fact")
		script.Parent.ScreenGui.Frame.HintStamina.Visible=true
 elseif num == 3 then
		 print("you got Jump Attack Fact")
		script.Parent.ScreenGui.Frame.HintJump.Visible=true
		
		wait(12)
		script.Parent.ScreenGui.Frame.HintCriticalDamage.Visible=false
		script.Parent.ScreenGui.Frame.HintStamina.Visible=false
		script.Parent.ScreenGui.Frame.HintJump.Visible=false
		end
	end)
	

but after it picks a random one at the beginning every time I click its the first one over and over again it doesn't ever randomize after the first click help?

1 answer

✓ Accepted answer

The reason this doesn't work is because the random number was already chosen before the function. So your num is before the function so that means its randomized and then every time your function runs its always one number because the random number was before the function. To fix this, just put the local variable INSIDE the function, so each time the function runs, a random number is chosen, not the script you have which already has a chosen number before everything, and it stays like that forever.

Fixed Code:

-- move the local num inside the function
	local BrickEvent = game.ReplicatedStorage.Events.Gui
	 
	BrickEvent.OnClientEvent:Connect(function(plr)
local num = math.random(1,3)
-- random number inside function so that every time function runs there's random number
	if num == 1 then
	          print("you got Critical Hit Fact")
	        script.Parent.ScreenGui.Frame.HintCriticalDamage.Visible=true
	 
	 elseif num == 2 then
	         print("you got Stamina Fact")
	        script.Parent.ScreenGui.Frame.HintStamina.Visible=true
	 elseif num == 3 then
	         print("you got Jump Attack Fact")
	        script.Parent.ScreenGui.Frame.HintJump.Visible=true
	 
	        wait(12)
	        script.Parent.ScreenGui.Frame.HintCriticalDamage.Visible=false
	        script.Parent.ScreenGui.Frame.HintStamina.Visible=false
	        script.Parent.ScreenGui.Frame.HintJump.Visible=false
	        end
	    end)

3 comments

0

thank you ill try this

0

so this works but i keep getting the same one like twice in a row is there a way i can add a chance system so its really hard to get the same one

0

try math.randomseed(tick())

This discussion is preserved as it was. New answers, comments, and votes are closed.