Wait until everything is loaded for a script to fire

Hey so i was wondering if there was a way at the start of a new server waiting until the player, parts and etc have loaded in because if i put my timer at 60, by the time the game has loaded for me, the output reads 48 seconds (12 seconds to load) meaning i have to perfectly time the second set of rounds to be insync

To do that, you need to have a specific condition which you keep waiting for until it becomes true. Example:

local Players = game:GetService("Players")

repeat 
	task.wait(.2)
until (#Players:GetPlayers() > 0) --our condition

--code here runs after above condition is met, you can start the timer
3 Likes

The ContentProvider service gives us information about the content delivered to the client.

In particular, to check how much of the game the client has loaded we can use: ContentProvider | Roblox Creator Documentation

local CP = game:GetService("ContentProvider")

repeat 
	wait()
until CP.RequestQueueSize > 0 

-- everything after this will run following the load.

Note that this is all clientside since the assets of the map are being loaded and displayedon the client’s computer.

4 Likes

I’ve an idea, though I’m not sure how effective it’ll be – maybe have a one-time remote event which’s fired when a client joins. For example,

-- LocalScript
-- Run pre-loading code
local LoadGame_Remote = game.ReplicatedStorage:WaitForChild("Load_Game", 5)
if LoadGame_Remote then LoadGame_Remote:FireServer() end
script:Destroy()

-- Main script/Server script
game.ReplicatedStorage.Load_Game.OnServerEvent:Wait()
game.ReplicatedStorage.Load_Game:Destroy()
-- code

Realistically, this problem isn’t that big of a deal. A server will only ever start once, so only the first player will see this. Assuming this timer is for rounds, intermission, or something similar, you will have players joining at odd times anyway. So, this probably isn’t something you should spend time on as no player will complain about this.

What would be a good idea is having some sort of main menu of sorts, where a player can press play to be actively in the game. A player will only press play once they are ready to play the game. Only start the timer when there are enough “active” players in the game, and the game will only start with players who have loaded in and are ready. This also allows for built-in afk mode functionality, and prevents players from being thrown into a game before they load in.

2 Likes