So I’m trying to have it get if there’s one player left on a team, and then running the stop function when there is. Can someone help me figure this out?
Script:
local SS = game:GetService("ServerStorage")
local SSS = game:GetService("ServerScriptService")
local Players = game:GetService("Players")
local notpl = game:GetService("Teams"):FindFirstChild("Not Playing >:(")
local round = Rep.round
local minitime = Rep.minitime
local mininame = Rep.mininame
local stat = Rep.gamestatus
local games = SS:WaitForChild("minigames"):GetChildren()
local gameclone = "game"
local function start()
local mgame = games[math.random(#games)]
gameclone = mgame:Clone()
gameclone.Parent = workspace
print(mgame)
minitime.Value = gameclone.Time.Value
mininame.Value = gameclone.Name
task.wait(.5)
for _, plr in pairs(game.Players:GetChildren()) do
plr.Character.Humanoid.Sit = false
plr.Team = game.Teams.Playing
wait(.01)
plr:LoadCharacter()
end
end
local function stop()
gameclone:Destroy()
for _, plr in pairs(game.Players:GetChildren()) do
plr:LoadCharacter()
plr.Team = notpl
end
round = false
end
while true do
for i = 15, 0, -1 do
stat.Value = "Intermission: " .. i
wait(1)
end
start()
for i = minitime.Value, 0, -1 do
stat.Value = mininame.Value .. ": " .. i
wait(1)
end
stop()
local team1 = game:GetService("Teams"):FindFirstChild("Playing")
if team1 then
local playerCount = #team1:GetPlayers()
print(playerCount)
if playerCount == 1 then
stop()
print("1")
end
else
print("Team1 not found!")
end
wait(1)
end
This is a poor method to use because it yields a script. It’d be best to use team.PlayerRemoved instead.
Also, you’re trying to get the length of the Teams service
Technically, it would go above your while loop, but I wouldn’t honestly fret about using it over ro1bro’s method since you’ve already got a while loop. I just prefer to avoid them in situations when possible, but it’s more of a nitpick than anything. If you did want to use their method, you still can with no issues
local team = yourTeam -- replace this
team.PlayerRemoved:Connect(function()
if #team:GetPlayers() == 1 then
-- Code would go here!
end
end)
local Rep = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local SSS = game:GetService("ServerScriptService")
local Players = game:GetService("Players")
local teams = game:GetService("Teams")
local notpl = game:GetService("Teams"):FindFirstChild("Not Playing >:(")
local round = Rep.round
local minitime = Rep.minitime
local mininame = Rep.mininame
local stat = Rep.gamestatus
local playerss = Rep.players
local games = SS:WaitForChild("minigames"):GetChildren()
local gameclone = "game"
local function start()
local mgame = games[math.random(#games)]
gameclone = mgame:Clone()
gameclone.Parent = workspace
print(mgame)
minitime.Value = gameclone.Time.Value
mininame.Value = gameclone.Name
task.wait(.5)
for _, plr in pairs(game.Players:GetChildren()) do
plr.Character.Humanoid.Sit = false
plr.Team = game.Teams.Playing
wait(.01)
plr:LoadCharacter()
end
end
local function stop()
gameclone:Destroy()
for _, plr in pairs(game.Players:GetChildren()) do
plr:LoadCharacter()
plr.Team = notpl
end
round = false
end
local team = teams:FindFirstChild("Playing")
team.PlayerRemoved:Connect(function()
if #team:GetPlayers() == 1 then
stop()
print("one left")
end
end)
while true do
for i = 15, 0, -1 do
stat.Value = "Intermission: " .. i
wait(1)
end
start()
for i = minitime.Value, 0, -1 do
stat.Value = mininame.Value .. ": " .. i
wait(1)
end
stop()
end
Alright, so there’s two ways you could go about this.
The first would be to omit the team.PlayerRemoved function entirely and move the if statement into it into the second for loop in your while loop, and replace stop() in it with break.
The other way to do it would be to add an if statement to it to check if round is false, and if it is break. You’d also need to add an if statement to make sure round is true when stopping just so it doesn’t stop twice in a row. I recommend the first, so sorry if I’m making you jump around with code. In my original reply, I didn’t fully know what the use case was, so that’s why I was speaking against while loops, but for this scenario it makes sense,
Reading into your original code, the main reason it wasn’t running is because the second for loop was constantly waiting, not allowing the code below it to run. However, if you put the code inside the for loop, it’ll run every second and not be stopped
So I tried the first one, and it didn’t work. One player was on the Playing team, while the other was not despite one of them dying and leaving the team.
local Rep = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local SSS = game:GetService("ServerScriptService")
local Players = game:GetService("Players")
local teams = game:GetService("Teams")
local notpl = game:GetService("Teams"):FindFirstChild("Not Playing >:(")
local round = Rep.round
local minitime = Rep.minitime
local mininame = Rep.mininame
local stat = Rep.gamestatus
local playerss = Rep.players
local games = SS:WaitForChild("minigames"):GetChildren()
local gameclone = "game"
local function start()
local mgame = games[math.random(#games)]
gameclone = mgame:Clone()
gameclone.Parent = workspace
print(mgame)
minitime.Value = gameclone.Time.Value
mininame.Value = gameclone.Name
task.wait(.5)
for _, plr in pairs(game.Players:GetChildren()) do
plr.Character.Humanoid.Sit = false
plr.Team = game.Teams.Playing
wait(.01)
plr:LoadCharacter()
end
end
local function stop()
gameclone:Destroy()
for _, plr in pairs(game.Players:GetChildren()) do
plr:LoadCharacter()
plr.Team = notpl
end
round = false
end
local team = teams:FindFirstChild("Playing")
while true do
for i = 15, 0, -1 do
stat.Value = "Intermission: " .. i
wait(1)
end
start()
for i = minitime.Value, 0, -1 do
stat.Value = mininame.Value .. ": " .. i
wait(1)
end
stop()
if #team:GetPlayers() == 1 then
stop()
print("one left")
break
end
end
Yeah, you should be putting the if statement inside the second for loop. Since it’s always waiting, the if statement will only run once after the for loop.