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 3 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.

local lighting = game.Lighting
local button = script.Parent
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = button
ClickDetector.MaxActivationDistance = 15

ClickDetector.MouseClick:Connect(function()
    print("Button has beed clicked.")
    button.Transparency = 1
    game.Workspace.Stand.Transparency = 1
    game.Workspace.Stand.CanCollide = false
    button.CanCollide = false
    wait(0.4)
    lighting.FogEnd = 12 or game.Workspace.Gravity == 30
    if lighting.FogEnd == 12 then do
            game.Workspace.FindMe.Transparency = 0
            game.Workspace.FindMe.CanCollide = true
            game.Workspace.FindMe.ClickDetector.MouseClick:Connect(function()
                lighting.FogEnd = 1000
                button.CanCollide = true
                button.Transparency = 0
                game.Workspace.Stand.Transparency = 0
                game.Workspace.Stand.CanCollide = true
                game.Workspace.FindMe.CanCollide = false
                game.Workspace.FindMe.Transparency = 1
                if game.Workspace.Gravity == 30 then do
                        game.Workspace.World.BrickColor  = BrickColor.Blue()
                    end
                end
            end)
        end
    end
end) 

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 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:

local Random = math.random(1, 2)

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

Explanation of math.random:

local a = math.random(1, 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
-- 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:

local lighting = game.Lighting
local button = script.Parent
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = button
ClickDetector.MaxActivationDistance = 15

ClickDetector.MouseClick:Connect(function()
    print("Button has beed clicked.")
    button.Transparency = 1
    game.Workspace.Stand.Transparency = 1
    game.Workspace.Stand.CanCollide = false
    button.CanCollide = false
    wait(0.4)
    local variable = math.random(1, 2) -- Your variable

    if variable == 1 then
        -- Your code for Fog
    else
        -- Your code for Lightning
    end
end) 
0
Thanks, i am just trying this out, i'll let you know when i'm done TgaTheGoldenArmour 15 — 3y
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 — 3y
0
Well, to fix that, put that math.random() variable inside the MouseClick event. BestCreativeBoy 1395 — 3y
0
How would I put math.random() inside a mouseclick event? TgaTheGoldenArmour 15 — 3y
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 — 3y
0
Yeah, I admit, my answer was a little unclear. Sorry! deeskaalstickman649 475 — 3y
Ad
Log in to vote
0
Answered by 3 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