Checking whether or not all players have finished loading into the server for story game

Until every player has loaded in or until 60 seconds have passed I want every player who loaded in early to just see a blank screen with some text.

I found game:IsLoaded() on the documentation but I dont really understand it at all. I also dont understand what game.Loaded:Wait() does.

Alright. First, let’s talk about game:IsLoaded() and game.Loaded:Wait(), although these are not quite what you are looking for.

  • game:IsLoaded()
    • Returns true if the entire game has finished loading on the client
    • Good for checking if assets like maps, scripts, and GUIs are ready
    • Doesn’t tell you if other players have loaded in (“other players” because it’s client sided
  • game.Loaded:Wait()
    • Pauses the script until the game is fully loaded
    • Basically it’s preventing the code from running until everything is loaded
    • Again, this is client-side only - it doesn’t wait for other players

Alright. So I’m assuming you’re using TeleportAsync() to teleport your players. In order to check if all the players have loaded, you first need to know how many players are being teleported. You can do this with the :SetTeleportData method of TeleportOptions. Simply set the teleport data to a table containing the number of players. If you need help with this, you can find code examples here. You would just pass in the TeleportOptions instance as an argument in TeleportService:TeleportAsync(). To receive this data, you would use Player:GetJoinData (all of this on how to use SetTeleportData is in the link I provided by the way). Then, once you have the number of players, you would do something like

local elapsed
repeat 
   elapsed += task.wait(1)
until #game.Players:GetPlayers() == numberOfPlayers or elapsed > 60

For the GUI, you could just have a local script in a ScreenGui that does something similar and sets the ScreenGui.Enabled property to false after waiting for all the players. However, for the number of players on the client, you would use GetLocalPlayerTeleportData.

It’s in the name, it means is the game loaded or not (game:IsLoaded()), and should I stop serial execution and wait for the game to load (game.Loaded:Wait()).

But you should be checking if the player’s have loaded based on your code, and what not, not just roblox’s data model.

Start a countdown on the server, and wait for how many ever players need to load, to load.
This can be done by each player when done loading wtv, sends a remoteEvent to the server, then the server can mark them down as loaded, if the player leaves, you simply don’t wait on that player anymore.

I don’t know what your game is about or the context of what you’re loading, so that’s my advice.

Problem is, the server won’t know how many players need to load. That’s why you need TeleportData.

I don’t know the game’s context, he didn’t describe the context of the game.

Player count exists, game.Players:GetPlayers()
But, it can be inaccurate due to some players loading in before others. (that’s why you have TeleportData)

I was thinking of a regular server intermission countdown. Not a teleport to another place wait for everyone countdown.

Oh yeah. You’re right. It doesn’t specify that hes teleporting players however, I would assume that otherwise he wouldn’t need to wait for the players to load - if it’s an open-ended game (where players can hop right in the middle of the match), you wouldn’t be waiting for players or if it’s within the server that you “press play,” the players would already have to be loaded.

I also need to delay certain other server-side scripts until everyone has joined in but GetTeleportData seems to only work in a local script. How do I get the teleport data in a server script?

(also this is for a story game as I said in the title, it’s like Camping)

sigh

I actually mentioned this in my response.


Now this could be done in a few ways and it might be a little tricky,

I would say either

  1. Have all scripts disabled by default and have your main teleport handler enable all the scripts or
  2. Make all the scripts wait for a BoolValue or BindableEvent/RemoteEvent.

So I’d grab the teleportdata table every time someone joins?

shared.eotd = math.random(0,2)
shared.selectedPlayer = 1
shared.waitingFinished = false
shared.selectionFinished = false
local teleportData = 0
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
	if type(player:GetJoinData().TeleportData) == table then -- cause exploitres can change teleport data
		teleportData = player:GetJoinData().TeleportData
	end
end)
shared.TotalPlayers = teleportData
local elapsed = 0
repeat
	elapsed += task.wait(1)
until #game.Players:GetPlayers() == #shared.TotalPlayers or elapsed > 60
shared.waitingFinished = true
if shared.eotd == 2 then
	print("Waiting for players to load because a random player should be EOTD")
	local playerList = Players:GetPlayers()
	if #playerList > 0 then
		local randomIndex = math.random(1, #playerList)
		shared.selectedPlayer = playerList[randomIndex].UserId
		local selectedPlayerName = playerList[randomIndex].Name
		print("Random selected user is "..selectedPlayerName)
		shared.selectionFinished = true
	else warn("Player list was empty; no user selected. Defaulting to user id "..shared.selectedPlayer)
	end
end

Yeah, essentially. You don’t necessarily have to retrieve the teleport data for every player, but you could. Also,

This won’t exactly work because teleportData is a table, so either you would index it like teleportData.TotalPlayers or if you just make it a list of the UserIds, you could just do #teleportData (I believe #teleportData would work unless some extra data is sent along with your specified data); however, I recommend using the former option.

I forgot to mention this, but you could also use MemoryStores (essentially lightweight, temporary, and quick datastores - tutorial here) or MessagingService (used to send data across servers). With these, you can send the data for the number of players for the server ID and depending on how you’re teleporting the players, you could use game.JobId or game.PrivateServerId to check if the data is for your server.

Quite new to scripting so forgive me but I don’t really understand. You can’t assign tables to variables? What does “indexing it” mean?

Make player fire RemoteEvent.
If all players has fired RemoteEvent, game starts.

And give them like 60 seconds to do that or else game starts because of timeout.

He’s teleporting the players. He needs to get the player count so he can wait for all the player instances to actually be loaded into the game.

So use memory stores then?
Regardless its a terrible idea.
Not all players is guaranteed to teleport.

Althrough you can store JobId to give people second attempt to join.

You can. You can store anything in a variable. That’s the definition of a variable.

Basically, when you get the teleport data with GetJoinData.TeleportData, I believe it’s a table containing the values. So, when you do GetJoinData.TeleportData, it will return a table that looks something like this

{TotalPlayers = 5}

So to access this value, you need to do TeleportData.TotalPlayers.

Edit: I see that you’re comparing the length of game.Players:GetPlayers and the length of the teleport data. I have to correct myself here

You can’t do this because

  1. It’s a dictionary so the # operator doesn’t work on it
  2. Even if it did, you would only be getting the number of keys so (assuming # did work on dicts)
local teleportData = {TotalPlayers = 5}
print(#teleportData) --would print 1 IF it worked on dictionaries but it doesnt

Dont use join data
Its unsafe+unreliable.

Use player id (game host) for temporal memory store data.

I’m giving the OP options. Though I should’ve mentioned that TeleportData is unsafe. Doesn’t matter too much though with the 60 second time limit.

As I said this is for a story game like camping. All I want is to make sure none of the story starts before everyone has joined in, the most an exploiter could do is make it start early.

The table looks like this:

{
[1] = Noxturnum2
}

What’s a “dictionary” exactly? A type of table right? Is that what my table is?

Here’s the code of the teleporter, sorry I shouldve sent this earlier.

local TeleportService = game:GetService("TeleportService")
local SignLabel = script.Parent.Parent.Sign.Top.SurfaceGui.TextLabel
while true do
	local players = {}

	for i, v in pairs(script.Parent.Seats:GetChildren()) do
		if v.Occupant then
			local plr = game.Players:GetPlayerFromCharacter(v.Occupant.Parent)
			print(#players)
			if plr then
				table.insert(players,plr)
			end
		end
	end

	if #players > 0 then
		for i = 30,0,-1 do
			players = {}
			for i, v in pairs(script.Parent.Seats:GetChildren()) do
				if v.Occupant then
					local plr = game.Players:GetPlayerFromCharacter(v.Occupant.Parent)
					if plr then 
						table.insert(players,plr)
					end
					print(#players)
					print(players)
				end
			end
			if not (#players > 0) then
				SignLabel.Text = "Waiting for players"
				print("Loop broken")
				break
			end
			SignLabel.Text = "Leaving in "..i.." seconds"
			task.wait(1)
		end
		if #players > 0 then 
			for i = 0,150,1 do 
				script.Parent:TranslateBy(script.Parent.Hitbox.CFrame.lookVector)
				task.wait(0.01)
			end
			print(players)

			local serverData = TeleportService:ReserveServer(101045119784698)
			local teleportOptions = Instance.new("TeleportOptions")
			teleportOptions.ReservedServerAccessCode = serverData
			teleportOptions:SetTeleportData({players})

			TeleportService:TeleportAsync(101045119784698,players,teleportOptions)
			
			task.wait(10)

			for i = 0,150,1 do 
				script.Parent:TranslateBy(-script.Parent.Hitbox.CFrame.lookVector)
				task.wait(0.01)
			end
		end

	else do
			SignLabel.Text = "Waiting for players"
		end
	end
	-- Move truck

	task.wait(1) -- yield
end

I use # with my table in the teleporter code so I don’t think its a dictionary. I’m setting the teleport data to the table, then in the teleportee code grabbing that exact table and checking for the number of players in it with the # operator, like I did in the teleporter’s code. Would there be anything wrong with that?

Yeah, so when you do this,

you’re setting the teleport data to

{
    {
        Player1UserID,
        Player2UserID,
        Player3UserID
    {
}

So simply getting the length of the teleport data wouldn’t work - you would need to get the length of teleportData[1]. Also, it is recommended that you set teleport data to a dictionary.

You can think of dictionaries as tables, but with keys. So dictionaries are just a list of key, value pairs. What I mean by this is that each value has a specific key assigned to it. For example

-- Edit: changed "Apple" to "Mango"

-- Table
local table1 = {
    "Mango",
    "Sword",
    67
}

-- Dictionary
local dictionary1 = {
    Fruit = "Mango",
    Weapon = "Sword",
    Money = 67
}

In tables, the keys are just the indexes so in the above example, table1[1] would return "Mango", but dictionary1[1] would return nil. To access the “Mango” value, you would have to do dictionary1["Fruit"] or dictionary1.Fruit. The nice thing about dictionaries is that you don’t have to know the order of the values - you just have to know which key corresponds to which value, making it really clean to use. However, unlike tables, you cannot use the # operator to get the length of a dictionary. You must manually count it through a loop.

So, using a dictionary for the teleport data, we could just do

teleportOptions:SetTeleportData({TotalPlayers = #players})

This way, when retrieving the teleport data, you would simply do

teleportData.TotalPlayers

Edit 2: The name dictionary is a little self-explanatory. You have a key (the word), and then you have the value that corresponds to the key (the definition).