IF statement doesn't work

Hi Developers!
So I got a problem with if statement.
Sometimes it work but sometimes it doesn’t, and I don’t know why.

if #AlivePlayers == 1 and InRound.Value == true then
Status.Value = AlivePlayers[1].Name…" won!"
wait(1)
break

function gameTimer()
	for number, player in game.Players:GetPlayers() do -- Gets Players
		table.insert(AlivePlayers, player) -- Inserts Players
		print(AlivePlayers[1])

		player.Character.Humanoid.Died:Connect(function() -- Connects a event when they die
			table.remove(AlivePlayers, AlivePlayers[player]) -- Removes Player
			print(AlivePlayers[1])
		end)
	end
	while roundLength >= 1 do
		wait(1)
		roundLength -= 1
		InRound.Value = true
		Status.Value = "Game: "..roundLength.." seconds left!"
		
		if #AlivePlayers == 1 and InRound.Value == true then
		Status.Value = AlivePlayers[1].Name.." won!"
		wait(1)
		break
		
		elseif #AlivePlayers == 0 and InRound.Value == true then
			Status.Value = "Everyone died!"
			wait(1)
			break
		end
	end
	roundLength = 10
	InRound.Value = false
	wait(1)
	Status.Value = "END!"
	wait(1)
end

In what way, does it not work?

Basically the code is skipped and even if there is only 1 player loop doesn’t break.

Try print the statements right above the if statement

print(#AlivePlayers == 1, InRound.Value == true)

If both returns true, then they both worked and the if-statement was passed.

table.remove() takes in a position as the argument, not the actual thing there. This means players who die won’t actually be removed properly.

It printed “true” two times.
image

If it printed true twice, then these lines would run.
image

That’s true. It worked, but when next round started it didn’t worked even if it should.

Make sure to set the AlivePlayers table to the default again, at the end of the round. Otherwise you’re just leaving that person in the table for the next round. We can call it the “CleanUp” phase.

Oh it worked! I’m so dumb. Thank you so much!

No problem, also make sure to reset the “AlivePlayers” table, each time you loop through the players, to check who’s alive, before starting. Otherwise you’re again just creating duplicates.

Edit: Also you cannot use the Player.Name to remove something from a table. So I would recommend using a Dictionary instead.

AlivePlayers = {}

AlivePlayers[Player.Name] = true -- Inserts the player

AlivePlayers[Player.Name] = nil -- Remove the player

-- Make sure to make an if statement, to check if they're in the dictionary, before adding & removing them.

image
Like that, right?

No at this line, you would also reset the AlivePlayers… dictionary in this case.
image

Edit: Ops wait a sec. Something is wrong here with your code, instead do a repeat loop.

-- Outside the function
local AlivePlayers = {}

-- At the start, we're checking for players
repeat
	AlivePlayers = {}
	for _, Player in pairs(game.Players:GetPlayers()) do
		local Character = Player.Character
		if not Character then continue end
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		if not Humanoid or Humanoid.Health == 0 then continue end
		AlivePlayers[Player.Name] = true
	end)
until #AlivePlayers >= MinimumPlayersToStart

Also I would personally make a function, that returns the “AmountOfPlayingPlayersStillAlive”, that runs every second in the round.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.