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:

while wait(10) do -- any number works
local number = math.random(1,10)
if number == 1 then
print("doing things")
elseif number == 2 then
print("doing more things")
--and so on
end

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:

local modes = {}

function modes:Mode1()
    --Code for mode 1
end)
function modes:Mode2()
    --Code for mode 2
end)
function modes:Mode3()
    --Code for mode 3
end)

--Repeat

return modes

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

local modeModule = require(script:WaitForChild("The name of your module script here"))

while true do
    mode = math.random(1, the number of modes you have)
    if mode == 1 then
        modeModule:Mode1()
    elseif mode == 2 then
        modeModule:Mode2()
    elseif mode == 3 then
        modeModule:Mode3()
    end
end

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

Answer this question