How can I make a server close when someone leaves?

In my game there are 1 player servers, I scripted it in a way that it would stop working when the second person joined and I really don’t want to bother rescripting it. I figure there is probably a function or something to close a server so that every server will host one player and then close.

Servers should close automatically once the last player leaves.

2 Likes

Oh, I didn’t even know that. Well that’s a relief.

If you are really paranoid about someone joining a single-player server after someone left you could do something like this:

local serverLocked = false
game:GetService("Players").PlayerAdded:Connect(function(player)
	if (serverLocked) then
		
		-- You can force them out with a message:
		player:Kick("This server has already been used. Please re-join.")
		
		-- OR try to send them to a (hopefully) fresh new server:
		game:GetService("TeleportService"):Teleport(game.PlaceId, player)
		
	end
	serverLocked = true -- Lock the server once the first player has joined.
end)
2 Likes