Making last player standing round system

I’ve been trying to make a last player standing round-based system. I don’t want the rounds to go on forever if the players are alive. I already made a basic round system that changes the player’s team depending on if the round starts or ends. I want to use that to end the round when there are 1 player on the ‘Playing’ team. Here is my code, server script, ServerScriptService:

local replicatedStorage = game:GetService("ReplicatedStorage")
local Status = replicatedStorage:WaitForChild("Status")
local InGameBool = replicatedStorage:WaitForChild("InGame")
local MapsFolder = replicatedStorage:WaitForChild("Maps")
local Maps = MapsFolder:GetChildren()

InGameBool.Changed:Connect(function()
	if InGameBool.Value == true then
		
		local ChosenMap = Maps[math.random(1,#Maps)]
		local ClonedMap = ChosenMap:Clone()
		ClonedMap.Parent = game.Workspace
		wait(2)
		
		for _, player in pairs(game.Players:GetChildren()) do
			local char = player.Character or player.CharacterAdded:Wait()
			char.HumanoidRootPart.CFrame = CFrame.new(ClonedMap.Spawn.Position) -- Teleports
		end
		
	else
		for _, player in pairs(game.Players:GetChildren()) do
			local char = player.Character or player.CharacterAdded:Wait()
			char.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.Lobby.SpawnLocation.Position) -- Teleports
		end
			
			for i,v in pairs (MapsFolder:GetChildren()) do
				if game.Workspace:FindFirstChild(v.Name) then
					local obj = game.Workspace[v.Name]
					obj:Destroy()
				end
			end
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		player.Team = game.Teams.Lobby

	end)
end)

local function changeTimer()
	local players = game:GetService("Players"):GetChildren()
	InGameBool.Value = false
	for i = 10,0,-1 do 
			wait(1)
			Status.Value = "Intermission:"..i.." Seconds Before New Match!"
			if i == 0 then
				for i,v in pairs(players) do
					v.Team = game.Teams.Playing
				end
			end
		end
	InGameBool.Value = true
	for i = 10,0,-1 do 
		wait(1)
		Status.Value = "In Game "..i.." Seconds Left Before Game Ends!"
		if i == 0 then
			for i,v in pairs(players) do
				v.Team = game.Teams.Lobby
			end
		end
	end
end

while wait() do
	local playersOnTeam = game:GetService("Teams")["Playing"]:GetPlayers()
	local numberPlayersOnTeam = #playersOnTeam
	if numberPlayersOnTeam < 2 then
		changeTimer()
	end	
end

Feedback is appreciated!

1 Like

Instead of having a global while wait() do loop that constantly checks for the numbers of players playing, you could have a script with an event to check when a player dies/leaves and use that to call a function to the server to do the checking.

Also just another small thing I have OCD about in case you don’t know:

Using:

local variable = true
if variable then
--Code
end

Is the same as using:

local variable = true
if variable == true then
--Code
end

To check if the variable isn’t true. you could do:

local variable = false
if not variable then
--Code
end
1 Like

What do you mean by

like a separate script or a function?

Also there is an InGame bool value that determines if it’s intermission or in a round, so do I use that?

1 Like

A separate script that detects when the player leaves/dies that fires a bindable event to that function which replaces the need for a loop, choice’s yours on how you’d approach this

1 Like