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
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.
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.
No at this line, you would also reset the AlivePlayers… dictionary in this case.
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.