Checking Player's team's not ending game

HouseModule is irrelevant. It just removes houses.
The code:

local HouseModule = require(game:GetService("ServerStorage"):WaitForChild("HouseModule"))

while task.wait(1) do
	for index, value in pairs(game:GetService("Players"):GetPlayers()) do
		local CurrentPeopleOnTeam = #game.Players:GetPlayers()
		if value.Team == game.Teams.Lobby then
			CurrentPeopleOnTeam -= 1
			if CurrentPeopleOnTeam <= 1 then
				if value.Team == game.Teams.Playing then
					value:WaitForChild("leaderstats"):WaitForChild("Wins").Value += 1
				end
				local RemoveHouses = HouseModule.RemoveHouses()
				value.Team = game.Teams.Lobby
			end
		end
	end
end
local HouseModule = require(game:GetService("ServerStorage"):WaitForChild("HouseModule"))
local Plrs = game:GetService("Players")
local Teams = game:GetService("Teams")

while true do
    local CurrentPlrsOnTeam = #Plrs:GetPlayers()

    for _, Plr in pairs(Plrs:GetPlayers()) do
        local leaderstats = Plr:WaitForChild("leaderstats")
        local Wins = leaderstats:WaitForChild("Wins")

        if Plr.Team.Name == "Lobby" then
            CurrentPlrsOnTeam -= 1

            if CurrentPlrsOnTeam <= 1 and Plr.Team.Name == "Playing" then
                Wins.Value += 1

                local RemoveHouses = HouseModule.RemoveHouses()
                Plr.Team = Teams.Lobby

                return
            end

        end
    end
    task.wait(1)
end

A while wait do loop will always run forever, regardless if the game’s over or not

You have to break it by either calling break or return to end the loop

Then the loop will never run again. This script is independant.

I am repasting this message since I replied to the wrong person on accident, oops.

It would be best to change this to an event-based function. It is bad practice to use while true loops since they eat up processing time which may not be an issue NOW, but it will be later on.

I’m assuming that when the players respawn or otherwise fail the game and result in being on the lobby team, you are coding it to do that. What I would do is have an IntValue in ServerStorage that stores the value of #players on Lobby and #players on ingame, which is added or subtracted when you add them to those teams.

Then for this script, you could use :Changed() and check if the count is <= 1 and do what you need from there.

It’s quiet hard to understand what you mean. I don’t think any event I know would work here, and .Changed isn’t reliable.

This is also asking why the script isn’t working, which is never been mentioned.

Try this:

local HouseModule = require(game:GetService("ServerStorage"):WaitForChild("HouseModule"))
local Plrs = game:GetService("Players")
local Teams = game:GetService("Teams")
game:GetService("RunService").Stepped:Connect(function()
	local CurrentPlrsOnTeam = #Plrs:GetPlayers()
	--Get Current players
	for _, Plr in pairs(Plrs:GetPlayers()) do
		local leaderstats = Plr:WaitForChild("leaderstats")
		local Wins = leaderstats:WaitForChild("Wins")

		if Plr.Team.Name == "Lobby" then
			CurrentPlrsOnTeam -= 1
		end
	end
	--Check
	if CurrentPlrsOnTeam <= 1 then
		for _, Plr in pairs(Plrs:GetPlayers()) do
			local leaderstats = Plr:WaitForChild("leaderstats")
			local Wins = leaderstats:WaitForChild("Wins")
			if Plr.Team.Name == "Playing" then
				Wins.Value += 1
				local RemoveHouses = HouseModule.RemoveHouses()
				Plr.Team = Teams.Lobby
			end
		end
	end
end)

Seems to kinda work. I had to add a DB to it because RenderStepped would’ve fired again before the script can finish and gave the player multiple wins.
Current code:

local HouseModule = require(game:GetService("ServerStorage"):WaitForChild("HouseModule"))
local Plrs = game:GetService("Players")
local Teams = game:GetService("Teams")
local DB = false
game:GetService("RunService").Stepped:Connect(function()
	if DB == false then
		print("Hey!")
		DB = true
		print("Hey!")
		local CurrentPlrsOnTeam = #Plrs:GetPlayers()
	--Get Current players
		for _, Plr in pairs(Plrs:GetPlayers()) do
			local leaderstats = Plr:WaitForChild("leaderstats")
			local Wins = leaderstats:WaitForChild("Wins")
			if Plr.Team.Name == "Lobby" then
				CurrentPlrsOnTeam -= 1
			end
		end
	--Check
		if CurrentPlrsOnTeam <= 1 then
			game.ReplicatedStorage.GameStats.IsGameRunning.Value = false
			for _, Plr in pairs(Plrs:GetPlayers()) do
				local leaderstats = Plr:WaitForChild("leaderstats")
				local Wins = leaderstats:WaitForChild("Wins")
				if Plr.Team.Name == "Playing" then
					Wins.Value += 1
					local RemoveHouses = HouseModule.RemoveHouses()
					Plr.Team = Teams.Lobby
					DB = false
					break
				end
			end
		end
	end
end)

I removed the break because its an event not a loop.
It will cause an error.
image

I removed the break and it hasn’t changed. It still fails.

What’s the error that fails it?

It doesn’t error, When only one person is on “Playing” nothing changes

Try to print the CurrentPlrsOnTeam, and check the value.

I think the issue is it only runs once. I’m not sure, but when I test, it only prints it once.

local HouseModule = require(game:GetService("ServerStorage"):WaitForChild("HouseModule"))
local Plrs = game:GetService("Players")
local Teams = game:GetService("Teams")
local DB = false
game:GetService("RunService").Stepped:Connect(function()
	if DB == false then
		print("Hey!")
		DB = true
		print("Hey!")
		local CurrentPlrsOnTeam = #Plrs:GetPlayers()
	--Get Current players
		for _, Plr in pairs(Plrs:GetPlayers()) do
			local leaderstats = Plr:WaitForChild("leaderstats")
			local Wins = leaderstats:WaitForChild("Wins")
			if Plr.Team.Name == "Lobby" then
				CurrentPlrsOnTeam -= 1
			end
		end
	--Check
		if CurrentPlrsOnTeam <= 1 then
			game.ReplicatedStorage.GameStats.IsGameRunning.Value = false
			for _, Plr in pairs(Plrs:GetPlayers()) do
				local leaderstats = Plr:WaitForChild("leaderstats")
				local Wins = leaderstats:WaitForChild("Wins")
				if Plr.Team.Name == "Playing" then
					Wins.Value += 1
					local RemoveHouses = HouseModule.RemoveHouses()
					Plr.Team = Teams.Lobby
				end
			end
		end
	end
    DB = false
end)

The DB is at a condition. So it will run once if the condition is false.

Works again. But returned to the same issue, it gives 2-3 wins instead of only just 1.

Try this

local HouseModule = require(game:GetService("ServerStorage"):WaitForChild("HouseModule"))
local Plrs = game:GetService("Players")
local Teams = game:GetService("Teams")
local DB = false

game:GetService("RunService").Stepped:Connect(function()
	if DB == false then
		print("Hey!")
		DB = true
		local CurrentPlrsOnTeam = #Plrs:GetPlayers()
	
		--Get Current players
		
		for _, Plr in pairs(Plrs:GetPlayers()) do
			local leaderstats = Plr:WaitForChild("leaderstats")
			local Wins = leaderstats:WaitForChild("Wins")
			if Plr.Team.Name == "Lobby" then
				CurrentPlrsOnTeam -= 1
			end
		end
		
		--Check
	
		if CurrentPlrsOnTeam <= 1 then
			game.ReplicatedStorage.GameStats.IsGameRunning.Value = false
			for _, Plr in pairs(Plrs:GetPlayers()) do
				local leaderstats = Plr:WaitForChild("leaderstats")
				local Wins = leaderstats:WaitForChild("Wins")
				if Plr.Team.Name == "Playing" then
					Wins.Value += 1
					local RemoveHouses = HouseModule.RemoveHouses()
					Plr.Team = Teams.Lobby
				end
			end
		end

		DB = false
	end
end)

I Fixed the RemoveHouses so that it will only play once.
I located the DB, because It was wrong.

It will loop through the code unless it Disconnects the event.

Place it in a function.

Change it to this:

function Enable()
   local HouseModule = require(game:GetService("ServerStorage"):WaitForChild("HouseModule"))
local Plrs = game:GetService("Players")
local Teams = game:GetService("Teams")
local DB = false
game:GetService("RunService").Stepped:Connect(function()
	if DB == false then
		print("Hey!")
		DB = true
		print("Hey!")
		local CurrentPlrsOnTeam = #Plrs:GetPlayers()
		--Get Current players
		for _, Plr in pairs(Plrs:GetPlayers()) do
			local leaderstats = Plr:WaitForChild("leaderstats")
			local Wins = leaderstats:WaitForChild("Wins")
			if Plr.Team.Name == "Lobby" then
				CurrentPlrsOnTeam -= 1
			end
		end
		--Check
		if CurrentPlrsOnTeam <= 1 then
			game.ReplicatedStorage.GameStats.IsGameRunning.Value = false
			for _, Plr in pairs(Plrs:GetPlayers()) do
				local leaderstats = Plr:FindForChild("leaderstats")
				local Wins = leaderstats:FindForChild("Wins")
				if Plr.Team.Name == "Playing" then
					Wins.Value += 1
					Plr.Team = Teams.Lobby
				end
			end
			local RemoveHouses = HouseModule.RemoveHouses()
			Event:Disconnect()
		end
		DB = false
	end
end)
end)
Enable()
--call Enable() to activate the loop again.

Same result. No difference. (CharacterLimit)

Can you show us your current script? It’s supposed to work