Hello. I made a script that loops through intermission and game time, with pauses in the loop when one team wins (there is a little victory intermission). That worked fine, but I want to end the game when all of one team dies. To do this, I created a table of one team named the “runners”.
local runnersTable = game.Teams.Runners:GetPlayers()
I used an if statement to break the gametime loop if runnersTable == 0.
if #runnersTable == 0 then
break
end
The problem is, the loop breaks even though I have 1 player on the team. I don’t know why it breaks the loop. Any help would be greatly appreciated!
Perhaps you’re using the older array,
try this instead:
local runners = game.Teams.Runners
-- each time get the latest array (by that I mean not the one defined already previously, to get the current number of players in the team each time you check)
if #runnersTable:GetPlayers() == 0 then
break
end
I’m assuming you’re doing this within a loop.
Edit:
Read:
With the array being defined outside the do -- end block - the count would only be compared against a predefined value which could ultimately lead the evaluation to return true regardless of the current and actual count of elements within the array each time the comparison is made, I assumed only, that the code was structured this way.
If in an iteration you’re only accessing a variable’s value once in a scope (which is being done here as can be discerned through the first code posted) then indexing the value directly will in fact take about the same time (neither will be faster unless you’re accessing a value assigned to a variable multiple times through the variable, in which case local variables are better) .
Here is the entire script. I am showing the whole thing because I have no clue where it doesn’t work.
local roundLength = 60
local intermissionLength = 10
local victoryLength = 15
local deathLength = 5
local InRound = game.ReplicatedStorage.InRound
local lobbyspawn = game.Workspace.LobbyAreaSpawn
local gamespawn = game.Workspace.GameAreaSpawn
local deathspawn = game.Workspace.deathspawn
local status = game.ReplicatedStorage.Status
local won = game.ReplicatedStorage.Won
local died = game.ReplicatedStorage.Died
local music = game.Workspace.music
local music1 = game.Workspace.music_duplex
local music2 = game.Workspace.music_duplexwon
local trombone = game.Workspace.trombone
local runnersTable = game.Teams.Runners:GetPlayers()
local title = game.StarterGui.Menu.TitleScreen
game.Players.PlayerAdded:Connect(function(player)
local Neutral = game.Teams.Neutral
player.Team = Neutral
end)
InRound.Changed:Connect(function()
if InRound.Value == true then
local death = game.Teams.Death -- this sets the teams
local runnersTeam = game.Teams.Runners
local plrs = game.Players
local runners = {}
repeat wait(1) until #plrs:GetPlayers() > 0
local chosen = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]
for i, plr in pairs(plrs:GetChildren()) do
if plr ~= chosen then
table.insert(runners, plr)
plr.Team = runnersTeam
else
plr.Team = death
end
print("teams have been chosen")
end
for _, player in pairs(game.Players:GetChildren()) do -- this teleports the players
local char = player.Character
if player.Team == runnersTeam then
char.HumanoidRootPart.CFrame = gamespawn.CFrame
else
char.HumanoidRootPart.CFrame = deathspawn.CFrame
end
end
print("players have teleported to the game area")
else --if the round isn't going, this places everyone in the neutral team and teleports them to the lobby
local plrs = game.Players
local Neutral = game.Teams.Neutral
for i, plr in pairs(plrs:GetChildren()) do
plr.Team = Neutral
end
print("all players have been placed in neutral")
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char.HumanoidRootPart.CFrame = lobbyspawn.CFrame
end
print("players have teleported to the lobby")
end
end)
local function RoundTimer() --this is the timer for the rounds. It changes the text in a GUI as well
while wait() do
if won.Value == false then
music:Play()
music1:Stop()
for i = intermissionLength, 0, -1 do
InRound.Value = false
wait(1)
status.Value = "Intermission: ".. i .." seconds left!"
end
music:Stop()
music1:Play()
for i = roundLength, 0, -1 do
InRound.Value = true
if won.Value == true then
break
end
wait(1)
status.Value = "Game: ".. i .." seconds left!"
end
else
music:Stop()
music1:Stop()
music2:Play()
for i = victoryLength, 0, -1 do
wait(1)
status.Value = "The runners have beaten death!"
end
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char.HumanoidRootPart.CFrame = lobbyspawn.CFrame
end
won.Value = false
end
end
end
spawn(RoundTimer)
@XxELECTROFUSIONxX Yes I did it within a loop. When the game started, the intermission clock stayed on zero forever.
@loveicicIe No errors. Also do note that script is just the intermission and game time looping forever with a break when the runners win. I gave you that one because that is the script the last time it worked completely.