I cannot tell when to end my last-man-standing round system

Please tell me if this post is breaking a rule

So i’ve lately been trying to make a last-man-standing game which teleports everyone to a arena. The round ends when everyone but one person is killed. The problem is I cannot tell when there is only 1 person left. I need help with creating a script that fires a function when there’s only 1 person left.

Here’s what i’ve tried so far ( I planned to make it check every second if someones in a team )

Code that doesn't work
	repeat
		wait(1)
		for i, player in pairs(game.Teams.Fighting:GetPlayers()) do
			if player == nil then -- Checks if there's no one on the team
				script.Value.Value = false
			end
		end
	until script.Value.Value == false

Thank you for any help!

3 Likes

The code is quite expensive, knowing the loop is every second. Why don’t you utilize signals, for instance if a player dies?

If the player died, the team reduces by 1. You could for instance use Humanoid.Died or code into the sequence where you switch the player’s team.

1 Like

You can insert each player into a table, remove them once they’ve died and once the player count is equal to 1, you’ve got the last player alive.

1 Like

The problem with that is that the round script is a while true do loop. How would I do that in a while true loop without it repeatedly doing Humanoid.Died?

Entire round code
local Players = game.Players

while true do
	wait(30)
	-- intermsiion--
	script.IntermisionTime.Value = 15
	repeat
		wait(1)
		script.IntermisionTime.Value = script.IntermisionTime.Value - 1
		for i, player in pairs(Players:GetPlayers()) do
			player.PlayerGui.MainGui.MainTextLabel.Text = script.IntermisionTime.Value
		end
	until script.IntermisionTime.Value < 1
	-- -- -- -- --
	-- teleports playeres into arena-
	for i, player in pairs(Players:GetPlayers()) do
		local Spawn = script:FindFirstChild("Spawn".. math.random(1,10))
		player.Character.HumanoidRootPart.Position = Vector3.new(Spawn.Position.X, Spawn.Position.Y, Spawn.Position.Z)	
		player.Team = game.Teams.Fighting
	end
	----
	
	-- checks if team is empty--
	script.Value.Value = true
	repeat
		wait(1)
		for i, player in pairs(game.Teams.Fighting:GetPlayers()) do
			if player == nil then
				script.Value.Value = false
			end
		end
	until script.Value.Value == false
	---------------------------
	print("hi") -- just a print
	
end

Thank you for the help! ( Sorry if the script is messy )

1 Like

When the round starts, make a table and add all the players to the table. Then use Humanoid.Died to check when a player has died. Once they have died, remove them from the table. Then check once there is only 1 player in the table and end the round.

3 Likes

I think you could get length of table using this operator: #

Example:

local table = {"a",2,"A"}
print(#table) --it should print 3

That means you can change code like this:

repeat
	wait(1)
until #game.Teams.Fighting:GetPlayers() < 2

But once player dies, you have to put player into another team.

2 Likes

I think i’ll try that, thank you!

1 Like

I haven’t tested it yet but I think that’s the best solution. Thank you!