I’m wondering about the order in which Roblox initializes instances and runs server scripts.
When the server starts, are all instances that exist in the place (e.g. ServerStorage, ReplicatedStorage, Workspace, etc.) guaranteed to already exist before any server side scripts begin executing?
For example, if a Script accesses an object in ServerStorage using FindFirstChild, is it safe to assume that object already exists, or are there situations where the script could run before certain instances have been initialized?
I’m trying to understand whether I should always use WaitForChild during initialization or if it’s only necessary for instances that may be created dynamically later.
No, its not guaranteed. Roblox deserializes the DataModel and starts running scripts in parallel there’s no strict “all instances load first, then scripts execute” contract. In most small-to-medium places youll get away with FindFirstChild because deserialization finishes fast, but its not safe to rely on that.
Example:
local obj = game:GetService("ServerStorage"):WaitForChild("MyObject", 10)
if not obj then
warn("MyObject failed to load within timeout")
return
end
The timeout parameter is important a bare WaitForChild without a timeout will yield forever if the instance never appears, which is a silent bug thats hard to track down.
Depends .. it is best to add the waitforchild. It doesn’t cost anything if it just hits it right away, if not you’ll error on a direct reference without it..
You don’t need WFC for instances that you reference from the server, so for your example where there are objects in server storage that are pre set instances, you wouldn’t need it. You don’t actually ever need to use WFC on the server, just FFC or directly index the reference. You only need to use WFC on the client
So statically set instances (set before running the game in the place file) will always be there when the script runs, removing the need of WFC. Right?
Yes, specifically on the server and any modules called from the server, the instances are guaranteed to exist everywhere. WFC is only for the client where instances loaded in before the script runs is not guaranteed
well he is right it works in server (in old posts kampfkarren and fmtrick said same thing). but like i said not “guaranteed” from docs (there is not any details about server or client).
Yeah I haven’t found anything in the docs saying it loads everything in, but like I’m 100% sure it does because I’ve never had a loading error from the server side compared to the client side