game:BindToClose() not working on more than 1 players

hi, i maked minimal shutdown script but script not working on game have more than 1 players.

local teleportService = game:GetService("TeleportService")
game:BindToClose(function()
	task.wait(1)
	game.ReplicatedStorage.Shutdown:FireAllClients() --for shutdown gui.
	teleportService:TeleportAsync(game.PlaceId, game.Players:GetPlayers())
	while game.Players:GetPlayers()[1] do
		task.wait(1)
	end
end)

image

1 Like

This might break as the first player index may be removed before other players and shut down the server as a result
Replace that with this instead

repeat wait() until #game.Players:GetPlayers() <= 0
1 Like

same problem, when server have more than 1 player, not working

If youre testing in studio, bindtoclose may have some weird functionality. Try testing in the actual game.

The while loop is not the problem, as getplayers returns an array so if the first player leaves the second player would become the new first player.

1 Like

If testing in studio then the teleport should’nt even work

i dont testing in studio.
roblox min character

It’s possible that game.Players:GetPlayers() runs once, causing what @neweve2323 mentioned. Try rewriting the loop in the following way to check if that’s the case:

while task.wait(1) do
	if #game.Players:GetPlayers() == 0 then break end
end

it’s not working when server have more than 1 player
:sob: i will cry