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

Make a single script run first?

Asked by 9 years ago

I have a script which I use to control a lot of things, though there are some lesser scripts that use this script. Knowing this, how can I ensure my first script runs before all of my other ones. I have tried putting wait() in front of the others, but is there a simpler way?

2 answers

Log in to vote
0
Answered by
Marios2 360 Moderation Voter
9 years ago

That is easy peasy.

Here's what it can be like: disable all scripts you want NOT to run yet, then re-enable the scripts inside the main script like this:

--Things and stuff you execute before
game.ServerScriptService.ServerScript.Disabled = false
workspace.WorkspaceScript.Disabled = false --You basically enable the disabled scripts in the script itself whenever you want to
--Things and stuff you execute before
Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Semafore

You should wait explicitly for some signal that the first is done.

For instance, if the first script is generating the world, then it could create a new value named something in ServerStorage:

-- Script that needs to run first
for i = 1, 100 do
    -- generate lots of stuff
    wait()
end

local done = Instance.new("NumberValue")
done.Name = "Generated"
done.Parent = game.ServerStorage
-- Script that needs to run after the first is done
game.ServerStorage:WaitForChild("Generated")

-- do whatever now

ModuleScript

Of course, in many of these cases, you could just use a ModuleScript. It's often better to design to directly interact with your dependencies (e.g., calling their functions) rather than just letting them set things up in the background and then searching out those thing.

Even if you aren't going to change the way it works, ModuleScripts have the nice properties that they will only run once (per client), and that you have to wait for them to run before continuing!

So, each script that needs something done before it can just require that module script:

-- (ModuleScript that needs to be run first)

for i = 1, 100 do
    wait()
end
-- do whatever
-- Other script that needs to happen after the first script happens
require(game.ServerScriptService.ModuleScript)

-- do whatever

The caveat of this is that you actually require something to come after it for it to run (or you can just stick a script inside of it which only requires it to be safe)

Answer this question