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

How do i fix this math random visibility issue?

local random1 = math.random(1,2) local random2 = math.random(1,2) if math.random(2,2) then game.StarterGui.kl.ok.Visible = false -- ok is frame name if math.random(1,1) then game.StarterGui.kl.ok.Visible = true end end

I put a frame under a gui and a local script in the gui this is my script, but idk why it doesnt work, its supposed to make the frames visibility false if math random is 1 and true if math random 2 would make the visibility true i have a basic idea of why it wouldn't work but can't come up with anything, any help ?

1 comment

0

dont mind that script this is the one local rand2 = math.random(1,2) print(rand2) if math.random(2) then print(rand2) wait(1) game.StarterGui.kl.ok.visible = false end

2 answers

Well first, you should put your code into code block, there is a button with lua icon for that, should create this:


your code here

~~~~~~

But to your question, i see you are doing

local random1 = math.random(1,2) local random2 = math.random(1,2)

if math.random(2, 2) then ...

Which line 4 has nothing to do with 2 previous variables you created, `math.random(2, 2)` will always return 2.
Instead you should check the variable:

if random1 == 1 then ...

So you could do something like this:

if random1 == 1 then game.StarterGui.kl.ok.Visible = true else -- random is not 1, so it's obviously 2 game.StarterGui.kl.ok.Visible = false end

This should work but here is also a cool way of doing it by using `and` and `or` statements, here is example in your case, you can just do this in one line and it will work.

game.StarterGui.kl.ok.Visible = (random1 == 1 and true) or false

Here is what will happen, first, it will check if random1 is 1, if it is then it will set the visible to true but if not, then it will jump to the or statement and set it to false.

Another issue why yours could not be working is because the gui object has not loaded yet, this is what [WaitForChild](https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild) is for, it is used on Gui object almost all the time. If that is the case then error could be something like `Attempt to index nil with ...`.




First, as suggested by imKirda, you have to put your code in :


-- YOUR CODE

There is a Blue Lua button for that.

Second, also as suggested by imKirda, you can't use math.random(2,2), as it will return 2 in every case. This was the code suggested by imKirda :

local random1 = math.random(1, 2)

if random1 == 1 then
	-- Do your stuff if you want it for 1
else
	-- Do your stuff if you want it for 2
end

-- Here's a little explanation for math.random()
local a = math.random(1, 5) -- So, it WILL NOT pick random number between 1 and 5. It WILL pick up number between 1, 2, 3, 4 and 5. So, the first parameter is lowest number and second parameter is highest number. ALSO, DON'T DO IT LIKE :
local a = math.random(5, 1) --It will error

Now, the main thing, don't use game.StarterGui, instead try indexing using, script.Parent... or player:WaitForChild("PlayerGui"). This is because, the contents of StarterGui gets copied to PlayerGui, when the player loads in.

So, this is how your code might look :

local player = game.Players.LocalPlayer -- It will get your player instance
local PlayerGui = player:WaitForChild("PlayerGui")

local random1 = math.random(1, 2)

if random1 == 1 then -- If the random number selected is 1
	PlayerGui.kl.ok.Visible = false
else -- There's no other possibility of another number, other than two.
--[[ elseif random1 == 2 then, If you want it for specific or there are more than two numbers to be randomized ]]
	PlayerGui.kl.ok.Visible = false
end

-- WARNING : THE CODE ABOVE WILL EXECUTE ONLY ONCE PER PLAYER RESPAWN, BECAUSE THERE IS NO EVENT, LOOP OR TRIGGER INCLUDED.

Lemme know if it helps!

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