How to teleport players once all reach destination

So basically I have a script that teleports players in game, inside a elevator then the elevator doors close.
How would I make it so that the doors would only close and script stuff happens, once every player in game has been in the elevator.

script for entering elevator:

script.Parent.Touched:Connect(function(other)
	print("Touched By:", other.Name)
	if other.Name == "Torso" then
		other.Parent.HumanoidRootPart.CFrame = game.Workspace.ElevatorTpPart1.CFrame
		
	end
end)

You should make a table, and store the players who touched that brick- and stayed there( make sure to add them into that table only if they havent been added already). And if the player leaves that region, remove him from the table. Eventually, you can check if every player appears in your table. If so, close the door. Etc

If so how would it know the total number of players to wait for?

But for my game the amount of players playing will vary,so each game if their was people playing it would be different.

local players = game:GetService("Players")

local startPart = workspace.TeleportFrom
local endPart = workspace.TeleportTo

local readyPlayers = {}

startPart.Touched:Connect(function(hit)
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if not hitModel then return end
	local hitPlayer = players:GetPlayerFromCharacter(hitModel)
	if not hitPlayer then return end
	local hitHuman = hitModel:FindFirstChildOfClass("Humanoid")
	if not hitHuman then return end
	if hitHuman.Health <= 0 then return end
	if table.find(readyPlayers, hitPlayer) then return end
	table.insert(readyPlayers, hitPlayer)
	if #readyPlayers == #players:GetPlayers() then
		for _, readyPlayer in ipairs(readyPlayers) do
			local readyCharacter = readyPlayer.Character
			if not readyCharacter then continue end
			readyCharacter:PivotTo(endPart.CFrame)
		end	
	end
end)

Just a bit of pseudo-code for you to work with, feel free to implement a minumum player condition.

1 Like