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

How do I make a randomiser that activates a script?

Asked by 6 years ago

I have a script that enables the rain, and I want it to randomly turn on, and off. To do this I need a random number every X minutes, and if it is <10. Then the rain turns on/off..

But I haven't been able to figure the Random Number Generator out, can someone please help me out?

Thanks in advance.

0
Use math.random FullMetalEdward45221 106 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

You can use the function math.random(min, max) to generate a number, where min is the minimum number, and max is the maximum number which the function can return any number between (inclusively)

i.e. math.random(1, 3) can return the numbers: 1, 2, or 3

An example of this with your scenario would be:

01local min = 5 * 60 -- To determine time of (X = 5) minutes in seconds
02local rain = script.Parent:WaitForChild("RainScript")
03 
04while true do -- Whatever loop you want to use
05    wait(min)
06    local random = math.random(0, 20)
07    if random < 10 then
08        if rain.Disabled == true then
09            rain.Disabled = false
10        else
11            rain.Disabled = true
12        end
13    end
14end
0
pls review how logic operators work. rain.Disabled == true XD User#5423 17 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
01local number = 0
02local raining = false
03 
04while true do
05    wait(300)
06    number = math.random(1,20)
07end
08 
09if number >10 then
10    raining = true
11else
12    raining = false
13end

I'm not entirely sure if this is what you wanted but every 5 minutes it will choose a random number between 1 and 20. If the number is less than ten then the raining variable will be false, and if it is greater than ten it will be true.

0
placing the if-then statement outside of the loop will make this only run once, when the script is created on the server SerpentineKing 3885 — 6y

Answer this question