How can we detect if there are only 2 players left in the same part?

Hello, I wanted to know how can we detect if there are only 2 players left in the same part ?

Here the local script where I want to put the script :

local players = game.Players
local Text = script.Parent.Conteur
local x = 1

game.ReplicatedStorage.RemotesEvents.Start.OnClientEvent:Connect(function()

	
	while true do
	
	print("New Game Preparation")
	
	Text.Text = "Game Preparation"
	wait(x)
	Text.Text = "Round Starting Soon : 30"
	wait(x)
	Text.Text = "Round Starting Soon : 29"
	wait(x)
	Text.Text = "Round Starting Soon : 28"
	wait(x)
	Text.Text = "Round Starting Soon : 27"
	wait(x)
	Text.Text = "Round Starting Soon : 26"
	wait(x)
	Text.Text = "Round Starting Soon : 25"
	wait(x)
	Text.Text = "Round Starting Soon : 24"
	wait(x)
	Text.Text = "Round Starting Soon : 23"
	wait(x)
	Text.Text = "Round Starting Soon : 22"
	wait(x)
	Text.Text = "Round Starting Soon : 21"
	wait(x)
	Text.Text = "Round Starting Soon : 20"
	wait(x)
	Text.Text = "Round Starting Soon : 19"
	wait(x)
	Text.Text = "Round Starting Soon : 18"
	wait(x)
	Text.Text = "Round Starting Soon : 17"
	wait(x)
	Text.Text = "Round Starting Soon : 16"
	wait(x)
	Text.Text = "Round Starting Soon : 15"
	wait(x)
	Text.Text = "Round Starting Soon : 14"
	wait(x)
	Text.Text = "Round Starting Soon : 13"
	wait(x)
	Text.Text = "Round Starting Soon : 12"
	wait(x)
	Text.Text = "Round Starting Soon : 11"
	wait(x)
	Text.Text = "Round Starting Soon : 10"
	wait(x)
	Text.Text = "Round Starting Soon : 9"
	wait(x)
	Text.Text = "Round Starting Soon : 8"
	wait(x)
	Text.Text = "Round Starting Soon : 7"
	wait(x)
	Text.Text = "Round Starting Soon : 6"
	wait(x)
	Text.Text = "Round Starting Soon : 5"
	wait(x)
	Text.Text = "Round Starting Soon : 4"
	wait(x)
	Text.Text = "Round Starting Soon : 3"
	wait(x)
	Text.Text = "Round Starting Soon : 2"
	wait(x)
	Text.Text = "Round Starting Soon : 1"
	wait(x)
	Text.Text = "Round Starting Soon : Teleportation..."
	print("Teleportation")
	wait(3)

	for _, plr in pairs(game.Players:GetChildren()) do

		local xOffset = math.random(-5,5)
		local zOffset = math.random(-5,5)

		plr.Character.HumanoidRootPart.CFrame = CFrame.new(5.2 + xOffset, 6.812, 44.29 +zOffset) 
		print("Teleportation Succed")

	end

	Text.Text = ""
	wait(2)
	Text.Text = "Box Spaw in : 25"
	wait(x)
	Text.Text = "Box Spaw in : 24"
	wait(x)
	Text.Text = "Box Spaw in : 23"
	wait(x)
	Text.Text = "Box Spaw in : 22"
	wait(x)
	Text.Text = "Box Spaw in : 21"
	wait(x)
	Text.Text = "Box Spaw in : 20"
	wait(x)
	Text.Text = "Box Spaw in : 19"
	wait(x)
	Text.Text = "Box Spaw in : 18"
	wait(x)
	Text.Text = "Box Spaw in : 17"
	wait(x)
	Text.Text = "Box Spaw in : 16"
	wait(x)
	Text.Text = "Box Spaw in : 15"
	wait(x)
	Text.Text = "Box Spaw in : 14"
	wait(x)
	Text.Text = "Box Spaw in : 13"
	wait(x)
	Text.Text = "Box Spaw in : 12"
	wait(x)
	Text.Text = "Box Spaw in : 11"
	wait(x)
	Text.Text = "Box Spaw in : 10"
	wait(x)
	Text.Text = "Box Spaw in : 9"
	wait(x)
	Text.Text = "Box Spaw in : 8"
	wait(x)
	Text.Text = "Box Spaw in : 7"
	wait(x)
	Text.Text = "Box Spaw in : 6"
	wait(x)
	Text.Text = "Box Spaw in : 5"
	wait(x)
	Text.Text = "Box Spaw in : 4"
	wait(x)
	Text.Text = "Box Spaw in : 3"
	wait(x)
	Text.Text = "Box Spaw in : 2"
	wait(x)
	Text.Text = "Box Spaw in : 1"
	wait(x)
	Text.Text = "Done !"
	
	game.Workspace.BoxSpawnDispawn.Wood.WoodBox:Clone().Parent = game.Workspace
	game.ReplicatedStorage.BoxSpawnDispawn.Iron.IronBox:Clone().Parent = game.Workspace
	game.ReplicatedStorage.BoxSpawnDispawn.Gold.GoldBox:Clone().Parent = game.Workspace
	game.ReplicatedStorage.BoxSpawnDispawn.Ether.EtherBox:Clone().Parent = game.Workspace
		
		
	
	wait(160)
		
		-- Teleport last 2 players to new arena
			
		

	end

end)

But idk how locate if we have 2 last player on a part and teleport they to 2 different part to make 1 vs 1

Someone can help me?

-Tnks

Not exactly sure, something you might be able to do is do an if statement and if there are only 2 people in a “in game” team then you can set their locations to the new area.

A few notes:

Instead of all these lines,
Why not use a simple for loop?
Something like -

for y = 30,1,-1 do
  Text.Text = "Round Starting Soon : "..y
  task.wait(x)
end

Instead of wait, use task.wait

3.You can simply use # alongside any table you need, this would get the amount of the players/items in that table. [could be a team, etc]

  1. use a damn loop christ jesus
Text.Text = "Game Preperation"
task.wait(x)

for epoch = 30, 1, -1 do
  Text.Text = 'Round Starting Soon : '..tostring(epoch)
end

task.wait(3)
  1. also if you’re judging how many players are left from a part you are making a grave mistake
    use tables or teams, here is an example from using a team
local teams = game:GetService('Teams')
repeat task.wait() until #teams.Playing:GetPlayers() <= 2

this script waits until there are two people left

Thanks a lot for the loop !!!

isn’t it easier to get a share if there are only 2 players left in a part? Because I don’t want an event to happen when there are only 2 players left in the game but still alive (knowing that some players could have died and are alive in the spawn)

thats why you also remove people from the playing team when they are dead

could you make tutorial or script ? I never touched team on roblox

https://developer.roblox.com/en-us/api-reference/class/Team

scroll to the bottom and see some examples
study them closely