Not getting that there's one player left

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
1 Like
local teams = game.GetService(“Teams”)
while true do
task.wait()
if #teams.Playing == 1 then
stop()
end
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

how would I use that, I dunno where it would exactly go

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)

So I got it situated, but I doesn’t do anything when there’s one player on the team

Can you send your updated script?

Could you debug it by using prints and making sure that the variables are correct?

1 Like

Here you go

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
2 Likes

Yeah, I placed a print and I got nothing.

Does “one left” print? and also it might not detect it as it’s for “PlayerRemoving”, so it’ll only detect when someone leaves the team.

1 Like

This would be intended since the number of players playing should not increase from 0 to 1

so it works now, but it doesn’t reset the timer (like it was supposed to), do you have any ideas on that?

By timer, do you mean the Intermission and Time left code at the bottom?

yeah, that’s the one I’m talking about

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.

Can you send the updated code?

Here you go

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.