Hi guys,
im attempting to make a round system that checks if there is one guy left or 0 players left and the way i do that is with roblox teams and a table called local playing = {}
Just take a look at my script:
local playingTeam = game.Teams.Playing
local lobbyTeam = game.Teams.Lobby
local intermission = 10
local roundLength = math.huge
local inRound = game.ReplicatedStorage.InRound
local statusofzo = game.ReplicatedStorage.Status
local function round()
while true do
local requiredPlayers = 2
repeat
wait(1)
statusofzo.Value = "At least ".. requiredPlayers.." players are needed to start a round"
until #game.Players:GetChildren() >= requiredPlayers
inRound.Value = false
for i = intermission, 0, -1 do
statusofzo.Value = "Game will start in "..i.." seconds"
wait(1)
end
inRound.Value = true
for i = roundLength, 0, -1 do
local playing = {}
for _, player in pairs(game.Players:GetChildren()) do
if player.Team.Name == "Playing" then
table.insert(playing, player.Name)
end
end
print("Number of players in Playing team:", #playing)
print("Contents of the playing table:", table.concat(playing, ", "))
task.wait(0.1)
if #playing == 0 then
statusofzo.Value = "Everyone Has Died"
wait(3)
break
end
statusofzo.Value = #playing .. " players left"
if #playing == 1 then
statusofzo.Value = playing[1].." Has Won The Game!"
for i, plr in pairs(game.Players:GetChildren()) do
if playing[1] == plr.Name then
plr.leaderstats.Wins.Value += 1
end
end
wait(3)
break
end
wait(1)
end
wait(3)
end
end
spawn(round)
i have honestly no idea as to why it just wont work if you see it please let me know.
The one thing i think i know is that the player doesnt get added into the playing table.
Just to let you know, this is a part of the script and the rest of the script works perfectly fine
My output looks like:
19:43:42.490 Contents of the playing table: - Server - RoundStuff:110
19:43:42.490 Player1 Lobby - Server - RoundStuff:19
19:43:42.491 Player2 Lobby - Server - RoundStuff:19
19:43:49.621 Player1 Playing - Server - RoundStuff:19
Instead of checking the amount of players using a table each second of the round length loop, you could use a repeat until loop instead so that the code will yield until the amount of players left is one. This has worked for me, but just in case, keep a backup of your old code.
Here is a sample:
repeat
wait(1)
until
#game.Teams.Alive:GetPlayers() <= 1
Then, if you want the winner, you can use GetPlayers() to get the player on the alive team, since they will be the only one left.
Hope this helps!
Edit:
I have put <= 1 just in case the player leaves directly on the round ending.
I’m not quite sure what you are trying to achieve.
Do you want the round to end when there is only one player left, or when there is none? If none, what do you want to happen after that?