Loop breaks even though it doesn't fit criteria

Hello. I made a script that loops through intermission and game time, with pauses in the loop when one team wins (there is a little victory intermission). That worked fine, but I want to end the game when all of one team dies. To do this, I created a table of one team named the “runners”.

local runnersTable = game.Teams.Runners:GetPlayers()

I used an if statement to break the gametime loop if runnersTable == 0.

if #runnersTable == 0 then
    break
end

The problem is, the loop breaks even though I have 1 player on the team. I don’t know why it breaks the loop. Any help would be greatly appreciated!

1 Like

I think you should add more seconds

It breaks because you break it inside the if statement to stop the loop you’re having above.
If the runnersTable’s length is == 0, it will break.

break is not valid if it is not inside a while / for loop.
Is that all of your code?

Perhaps you’re using the older array,
try this instead:

local runners =  game.Teams.Runners
-- each time get the latest array (by that I mean not the one defined already previously, to get the current number of players in the team each time you check)
if #runnersTable:GetPlayers() == 0 then
    break
end

I’m assuming you’re doing this within a loop.

Edit:

Read:

With the array being defined outside the do -- end block - the count would only be compared against a predefined value which could ultimately lead the evaluation to return true regardless of the current and actual count of elements within the array each time the comparison is made, I assumed only, that the code was structured this way.

If in an iteration you’re only accessing a variable’s value once in a scope (which is being done here as can be discerned through the first code posted) then indexing the value directly will in fact take about the same time (neither will be faster unless you’re accessing a value assigned to a variable multiple times through the variable, in which case local variables are better) .

That is no different, if anything: it is slower as assigning it as a local is faster.

Could you show more of the script where the loop is

Here is the entire script. I am showing the whole thing because I have no clue where it doesn’t work.

local roundLength = 60
local intermissionLength = 10
local victoryLength = 15
local deathLength = 5
local InRound = game.ReplicatedStorage.InRound
local lobbyspawn = game.Workspace.LobbyAreaSpawn
local gamespawn = game.Workspace.GameAreaSpawn
local deathspawn = game.Workspace.deathspawn
local status = game.ReplicatedStorage.Status
local won = game.ReplicatedStorage.Won
local died = game.ReplicatedStorage.Died

local music = game.Workspace.music
local music1 = game.Workspace.music_duplex
local music2 = game.Workspace.music_duplexwon
local trombone = game.Workspace.trombone

local runnersTable = game.Teams.Runners:GetPlayers()

local title = game.StarterGui.Menu.TitleScreen

game.Players.PlayerAdded:Connect(function(player)
	local Neutral = game.Teams.Neutral
    player.Team = Neutral
end)

InRound.Changed:Connect(function()
	if InRound.Value == true then	
		local death = game.Teams.Death -- this sets the teams
		local runnersTeam = game.Teams.Runners
		local plrs = game.Players
		local runners = {}
			repeat wait(1) until #plrs:GetPlayers() > 0
		local chosen = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]
		for i, plr in pairs(plrs:GetChildren()) do 
			if plr ~= chosen then
				table.insert(runners, plr)
				plr.Team = runnersTeam
			else
				plr.Team = death
			end
		print("teams have been chosen")
		end	
	
		for _, player in pairs(game.Players:GetChildren()) do -- this teleports the players
			local char = player.Character
			if player.Team == runnersTeam then
				char.HumanoidRootPart.CFrame = gamespawn.CFrame
			else
				char.HumanoidRootPart.CFrame = deathspawn.CFrame
			end
		end
		print("players have teleported to the game area")
	else --if the round isn't going, this places everyone in the neutral team and teleports them to the lobby
		local plrs = game.Players
		local Neutral = game.Teams.Neutral
		for i, plr in pairs(plrs:GetChildren()) do
			plr.Team = Neutral
		end
		print("all players have been placed in neutral")
		for _, player in pairs(game.Players:GetChildren()) do
			local char = player.Character
			char.HumanoidRootPart.CFrame = lobbyspawn.CFrame
		end		
		print("players have teleported to the lobby")
	end
end)

local function RoundTimer() --this is the timer for the rounds. It changes the text in a GUI as well
	while wait() do
		if won.Value == false then
			music:Play()
			music1:Stop()
			for i = intermissionLength, 0, -1 do
				InRound.Value = false
				wait(1)
				status.Value = "Intermission: ".. i .." seconds left!"
			end
			music:Stop()
			music1:Play()
			for i = roundLength, 0, -1 do
				InRound.Value = true		
				if won.Value == true then
					break
				end
				wait(1)
				status.Value = "Game: ".. i .." seconds left!"
			end
		else
			music:Stop()
			music1:Stop()
			music2:Play()
			for i = victoryLength, 0, -1 do
				wait(1)
				status.Value = "The runners have beaten death!"
			end

			for _, player in pairs(game.Players:GetChildren()) do
				local char = player.Character
				char.HumanoidRootPart.CFrame = lobbyspawn.CFrame
			end		
			won.Value = false
		end
	end
end

spawn(RoundTimer)

Do you get any errors in output at all?

@XxELECTROFUSIONxX Yes I did it within a loop. When the game started, the intermission clock stayed on zero forever.

@loveicicIe No errors. Also do note that script is just the intermission and game time looping forever with a break when the runners win. I gave you that one because that is the script the last time it worked completely.

Where in this script are you breaking the runners table loop if it hits 0?

All I see is a repeat wait

Edit: I put it inside here:

		for i = roundLength, 0, -1 do
			InRound.Value = true		
			if won.Value == true then
				break
			end
			wait(1)
			status.Value = "Game: ".. i .." seconds left!"
		end

It would go above the “if won” break.

Where is the game time loop, mind showing me where it starts?

Expected ‘end’ (to close ‘function’ at line 29), got ; did you forget to close ‘do’ at line 102?

I feel like there was just a typo somewhere, but I checked and didn’t see anything.

Wait hold on let me check really quick!

It should be working, I have the script up on studio and there are no end errors

For some reason it keeps giving me an error. I copied and pasted your script though.

Can I see the error in the output and make sure its just my script no other additions

Expected ‘end’ (to close ‘function’ at line 29), got ; did you forget to close ‘do’ at line 102?

this was the error. I copied and pasted your code on a new script, and disabled my original one with --[[ --]].

Try to fully disable yours using the Disabled option in the script, also what script is the error coming from??

I hit the disabled button. The error is coming from your script because when I click on the output error it takes me to a line in your code.