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

How do you make scripts execute in random order?

Asked by
OFF_S4LE 127
4 years ago

I'm trying to make scripts execute in random order, kinda like horrific housing, sorry if this is a request it was my last choice.

2 answers

Log in to vote
1
Answered by
quinzt 201 Moderation Voter
4 years ago

a way you could do this is by having one central script, with a math.random function. like this:

1while wait(10) do -- any number works
2local number = math.random(1,10)
3if number == 1 then
4print("doing things")
5elseif number == 2 then
6print("doing more things")
7--and so on
8end
Ad
Log in to vote
1
Answered by 4 years ago

Use a ModuleScript. You can read more about them here.

Basically, make one parented to your regular script. then do something like:

01local modes = {}
02 
03function modes:Mode1()
04    --Code for mode 1
05end)
06function modes:Mode2()
07    --Code for mode 2
08end)
09function modes:Mode3()
10    --Code for mode 3
11end)
12 
13--Repeat
14 
15return modes

Then, in your regular server, do something along the lines of:

01local modeModule = require(script:WaitForChild("The name of your module script here"))
02 
03while true do
04    mode = math.random(1, the number of modes you have)
05    if mode == 1 then
06        modeModule:Mode1()
07    elseif mode == 2 then
08        modeModule:Mode2()
09    elseif mode == 3 then
10        modeModule:Mode3()
11    end
12end

Hope this helps. Also I didn't test this, reply with errors!

Answer this question