Issue When Using "Break"

I’m working on a small-prototype for a potential video. It’s basically just a murder type game. The way it works is it just selects a random player, and sets a StringValue placed in ReplicatedStorage to the name of the player. [that value reprents the Current Killer] .

During the for i loop that is used to display the time left before the game ends, I check to see if the killer is still in the game, as well as if all players are still alive. Here is the script:

-- game time
		for i = GAME_TIME,0,-1 do
			statusUpdate(i)
			wait(1)
			if not game.Players[CurrentKiller.Value] then
				break
			end
			
			local playerTable = {}
			
			for i,v in pairs(game.Players:GetPlayers()) do
				if v.PlayerStats.Playing.Value == true then
					if v.Name ~= CurrentKiller.Value then
						table.insert(playerTable, v.Name)
					end
				end
			end
			
			if #playerTable <= 0 then
				for i = 1,#playerTable do -- clear table
					table.remove(playerTable, i)
				end
				break
			else
				for i = 1,#playerTable do -- clear table
					table.remove(playerTable, i)
				end
			end
		end

ISSUE:
For my first testing sessions, I test the killer leaving the game. When I leave the game, the timer stops, but the code doesn’t continue like I want it to. At the top of the code you can see it checks if the player exists in the game, and if not I use break.

This code is below that, which checks for who won [method is a little weird ik];

local winners = {}
		
		for i,v in pairs(game.Players:GetPlayers()) do
			if v.PlayerStats.Playing.Value == true then
				table.insert(winners, v.Name)
			end
		end
		
		if #winners == 1 then
			if winners[1] == CurrentKiller.Value then
				statusUpdate("The Killer Managed To Kill All Players!")
			else
				statusUpdate("Survivors Escaped!")
			end
		elseif #winners > 1 then
			statusUpdate("Survivors Escaped!")
		end
		
		cleanUp()

If anyone knows what could be wrong, please let me know. And yes I already used the Search Query to look for similar issues, but no luck.

1 Like

I think you are clearing your table incorrectly, because table.remove clears an entry from the array and then moves all values in the array back one from the entry removed:

local array = {10, 20, 30}
table.remove(array, 2)
table.foreach(array, print)
--[[
1 10
2 30

-- Notice that 30 is not at [3]!
--]]

Why don’t you just replace playerTable with a new table?

--[[ old code

if #playerTable <= 0 then
    for i = 1, #playerTable do
        table.remove(playerTable, i)
    end
    break
else
    for i = 1, #playerTable do
        table.remove(playerTable, i)
    end
end

--]]
if #playerTable <= 0 then
    break
end

-- clear table
playerTable = {}

I think I just realized it doesn’t matter because playerTable isn’t used again…

1 Like

You’re absolutely right, but that isn’t my issue.

I may have discovered an alternative let me try it out and get back to you.

1 Like

The problem is that break dosen’t break a for loop. Therefor it will break the while loop instead. I belive break only breaks while loops.

Indexing a Roblox object assumes that the key (child/property/etc.) already exists. In this case where you check if the player exists, you should use FindFirstChild instead, as this does not error even if the child does not exist. However, using your method, if a child (player) with that given name does not exist, it will error complaining that such a member does not exist, which ultimately stops your whole thread.

2 Likes

Quote from wiki article:

** If you have a while , for , or repeat loop that otherwise won’t end, you can program it to end with the break command so that you can continue with the next part of code**

I really thought that was gonna fix it, but it still had the same error. I put together an alternative script which just handles it directly in the for i loop. I’ll see how it goes.

You are getting an error? Please share the new script and the error message that you are getting with it.

1 Like

I shouldn’t have used error. It’s not an error, the code still stops.

if not game.Players:FindFirstChild(CurrentKiller.Value] then
     break
end

That is what I used.

It’s a loop, so it’s supposed to clear the table, thanks for the help. I probably would’ve had a tough time trying to figure that out too. :slight_smile:

1 Like

I put together an alternative method which works the intended way. For those of you who see this in the future, here is how I did it:


local gameEnded = false

for i = GAME_TIME,0,-1 do
			statusUpdate(i)
			wait(1)
			if not game.Players:FindFirstChild(CurrentKiller.Value) then
				statusUpdate("Killer Left!")
				cleanUp()
				gameEnded = true
				break
			elseif not game.Players:FindFirstChild(CurrentKiller.Value).Character then
				statusUpdate("Killer Died!")
				cleanUp()
				gameEnded = true
				break
			end
		end
		
		-- survivors win
		if gameEnded == false then
			statusUpdate("Survivors Managed To Survive!")
			cleanUp()
		end
2 Likes