How to detect if all players are loaded?

So I have a story game which I am working on, and I want to make sure everyone is loaded before starting up the game. Is there a definite way so I can wait for everyone to load without waiting too much time or waiting not enough time?

2 Likes

Your post is quite broad and not easily understandable. Please read the category guidelines to structure your post correctly.

Do you require a certain amount of players to be present before you begin the game? If so you can check if the number of players in the server are greater than or equal to your minimum.

if #game.Players:GetPlayers() >= 3 then
    -- we start the game
end

Off topic but for a story style game I reccomend keeping it single player. This way everyone can go along at their own pace and they don’t have to rely on other players to be present. Provides a better user experience.

1 Like

This is one of those games similar to camping.

You could do that; but it’d most likely take a long time. Specially if some of the players are laggy.

You would most likely need to add a timeout, otherwise long-lasting loading times are possible.

A feasible solution is to use the ContentProvider service to PreloadAsync any important assets you need loaded before the game starts and use a remote to communicate that with the server.

To make sure the players have loaded in, I recommend using ReplicatedFirst, as that service holds any asset loaded before the remaining assets.

To start: add a RemoteEvent in ReplicatedStorage, name it LoadEvent.

Add the following LocalScript to ReplicatedStorage:

local assets = {
  -- reference the assets you want loaded here, e.g.:
 game:service('Workspace'),
 game:service('StarterGui')
}

for i = 1, #assets do
  game:GetService("ContentProvider"):PreloadAsync({ assets[i] })
end

game:service('ReplicatedStorage'):WaitForChild('LoadEvent'):FireServer()

Then add the following script to ServerScriptService:

local MinimumPlayers = 3 -- Change this to how many players you want to join before loading waiting process begins

local Players = game:GetService("Players")

while #Players:GetPlayers() < MinimumPlayers do wait() end

local Loaded = 0
local MinimumLoad = #Players:GetPlayers()

-- MinimumLoad is now the amount of players in the server! This will ensure we wait for all of them to load first.

-- However, since that could bring the issue of very laggy players failing to load or leaving in the process, we need to add a timeout!

local timeout = 180 -- Change this to how many seconds until loading is made complete regardless of wether or not everyone has loaded.

game:GetService("ReplicatedStorage"):WaitForChild('LoadEvent').OnServerEvent:Connect(function(p)
  Loaded = Loaded + 1
end)

local start = tick()

while Loaded < MinimumLoad and (tick() - start) < timeout do wait() end

-- Now you know that everyone has loaded in! Maybe use a BindableFunction to start the game script itself from here on. This is now up to you.

Hope this helps you out!

Cheers.

19 Likes

I know it’s just an example and it’s nitpicky to say this, but please don’t actually ever preload the entire Workspace. Someone here is going to do that and I want to let that future person know that all preload does is push assets to the front of the download queue. Pushing everything to the front doesn’t result in any benefits.

9 Likes