What do you want to achieve?
Hello! I am making a round based system for my game, and I am trying to make it so that if there is only 1 player left alive, the round would end and they would be the winner.
What is the issue?
The round just ends after it starts.
What solutions have you tried so far?
I have tried putting the amount of players left in a table, however that doesnt work.
Here are some snippets of the code
for _, Player in pairs(game.Players:GetPlayers())do
...
local players = game:GetService("Players"):GetPlayers()
table.insert(Playing, table.find(Playing, players))
for _, Player in pairs(game.Players:GetChildren()) do
...
if Player.Character and Player.Character:FindFirstChild("Humanoid") then
Player.Character.Humanoid.Died:Connect(function()
table.remove(Playing, table.find(Playing,Player))
...
if #Playing <= 1 then
print("a player has won")
It doesn’t seem to work. Here’s where I put it if you need more info.
for i = 60, 0, -1 do
Status.Value = i
task.wait(1)
for _, Player in pairs(game.Players:GetChildren()) do
if Player.Character and Player.Character:FindFirstChild("Humanoid") then
connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
table.remove(Playing, table.find(Playing,Player))
end)
end
game.Players.PlayerRemoving:Connect(function(player)
local ind = table.find(Playing, player)
if ind then
table.remove(Playing, ind)
end
end)
end
for _, Player in pairs(Playing) do
if #Player == 1 then
print(Playing, "Has won")
RoundEndSFX: Play()
MusicStart:Stop()
MusicLoop:Stop()
break
end
end
end
for i = 60, 0, -1 do
Status.Value = i
task.wait(1)
for _, Player in pairs(game.Players:GetPlayers()) do
if Player.Character and Player.Character:FindFirstChildOfClass("Humanoid") then
connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
table.remove(Playing, table.find(Playing,Player))
end)
end
game.Players.PlayerRemoving:Connect(function(player)
local ind = table.find(Playing, player)
if ind then
table.remove(Playing, ind)
end
end)
end
for _, Player in pairs(Playing) do
if #Player == 1 then
print(Playing, "Has won")
RoundEndSFX:Play()
MusicStart:Stop()
MusicLoop:Stop()
break
end
end
end
Looking at it from a high level, I would create a property attribute for each player via leaderstats, then change that property every time a player dies. Create a loop that goes through all of players in game:GetPlayers() every couple seconds, and create a running count for each iteration of the loop which tracks how many are alive. When the count is just one, go through the list of players again, and when you found the player with the property, you can store it in a different variable and use it as needed.
You can clear out all the values after each round ends.
I’m kind of dumb so I don’t know if this would work.
You could make a value called players alive, and when the round starts you check how many people there are and make the value the amount of people. Then, make a value called in game that can be inside each player and so when the game starts it is true and when they die it’s set to false. Then, when the player dies the value is set to false and the number of players value decreases. This would repeat until it detects there is one player left.
That’s along the lines of what I was thinking, except instead of checking player count, I would probably just add 1 to each whenever you tp the player or wtv so that you can have some kind of feature that allows some players to not join.