Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Randomly pick out of 2 options?

Asked by 4 years ago

Hello everyone, I am making a roblox game where when you press a button on of 2 things will happen, those 2 things are either the game can be foggy or the game could have low gravity, want it to randomly pick one of those options each time, the code below is all of the code in the button (the one that i press to make this happen) it is inside a normal script, i tried a or statement thinking it would choose between the 2 options but it seems to only always pick the first option, how would i make it randomly choose between the 2 instead of always picking the first option using the code below, i tried a or statement as you can see below but that didn't seem to work. Line 14 is where i used the statement to randomly pick between the 2 but it didn't work, any help would be great, thank you.

01local lighting = game.Lighting
02local button = script.Parent
03local ClickDetector = Instance.new("ClickDetector")
04ClickDetector.Parent = button
05ClickDetector.MaxActivationDistance = 15
06 
07ClickDetector.MouseClick:Connect(function()
08    print("Button has beed clicked.")
09    button.Transparency = 1
10    game.Workspace.Stand.Transparency = 1
11    game.Workspace.Stand.CanCollide = false
12    button.CanCollide = false
13    wait(0.4)
14    lighting.FogEnd = 12 or game.Workspace.Gravity == 30
15    if lighting.FogEnd == 12 then do
View all 33 lines...

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Well, let me elaborate what @deeskaalstickman649 is trying to say. Basically, you cannot use anything in math.random(). It only supports number. So, if you want to apply math.random in your code, you can do:

1local Random = math.random(1, 2)
2 
3if Random == 1 then
4    -- What you want to do (E.G.: Fog script)
5else -- Using else, as there are no other possible outputs than 1 or 2
6    -- (E.G.: Lightning script)
7end

Explanation of math.random:

1local a = math.random(1, 2)
2-- Structure of math.random() is math.random(min. no., max. no.). WARNING : DO NOT DO like -- math.random(2, 1), it will error, as the first parameter is the minimum number
3-- Variable 'a' is assigned for the result of the random between 1 and 2

If you still have doubts, comment down and I will try my best to help.

Lemme know if it helps!

EDIT : If you are getting confused, on putting the variable for math.random(), here, you should put it here:

01local lighting = game.Lighting
02local button = script.Parent
03local ClickDetector = Instance.new("ClickDetector")
04ClickDetector.Parent = button
05ClickDetector.MaxActivationDistance = 15
06 
07ClickDetector.MouseClick:Connect(function()
08    print("Button has beed clicked.")
09    button.Transparency = 1
10    game.Workspace.Stand.Transparency = 1
11    game.Workspace.Stand.CanCollide = false
12    button.CanCollide = false
13    wait(0.4)
14    local variable = math.random(1, 2) -- Your variable
15 
View all 21 lines...
0
Thanks, i am just trying this out, i'll let you know when i'm done TgaTheGoldenArmour 15 — 4y
0
Thanks, this fixed that issue but added another, in this game you will be able do press this button multiple times so it always has to be random, this makes it random once, how could i fix that? Thanks though for helping me with the first part. TgaTheGoldenArmour 15 — 4y
0
Well, to fix that, put that math.random() variable inside the MouseClick event. BestCreativeBoy 1395 — 4y
0
How would I put math.random() inside a mouseclick event? TgaTheGoldenArmour 15 — 4y
View all comments (2 more)
0
You might be assigning variable for math.random(). Just put that variable in MouseClick event of ClickDetector. I just edited by code, just for clearance. BestCreativeBoy 1395 — 4y
0
Yeah, I admit, my answer was a little unclear. Sorry! deeskaalstickman649 475 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Use math.random() to generate a random number, and then use an if statement to make the game foggy or low gravity depending on the returned number. Make sure to use the math.random() inside the event.

Answer this question