Trying to make it so that when there is less than 2 players, the intermission/game stops

Hello! I am currently trying to script that when 2 players join the game, the game begins, but when there is less than two players, it changes the text to “Need at least 1 more player to start”.

My problem is that say I join with 3 local players, if 2 of them leave, for a single second it will say “Need at least 1 more player” but the intermission still plays.

Anyone know a solution? I will provide all scripts and a video of the problem below <3

Problem:

Main Script
local gameStatusModule = require(game.ReplicatedStorage.GameStatusModule)
local inRound = false
local enoughPlayers
local plrsInGame = 0

local isEnoughPlayers = gameStatusModule.notEnoughPlayers

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		plrsInGame += 1
		print("There are ".. tostring(plrsInGame))
		--while true do
		wait()
		if plrsInGame < 2  then
			enoughPlayers = false
				gameStatusModule.notEnoughPlayers2("Need at least 1 more player to start!", plrsInGame)
			elseif plrsInGame >= 2 then
				enoughPlayers = true
		end
		if plrsInGame >= 2 and inRound == false and enoughPlayers == true then
			while true do
				warn("run intermission")
				gameStatusModule.intermission("Intermission: ", plrsInGame)
				local isIntermissionEnd = gameStatusModule.intermission
				if isIntermissionEnd then -- if returns true (when function is finished it will begin the next line of code)
					warn("run game")
					inRound = true
					gameStatusModule.startGame("Drawer has ")
					local isGameEnd = gameStatusModule.startGame
					if gameStatusModule then
						warn("game ended")
						inRound = false
						wait(1)
					end
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	plrsInGame -= 1
	print("There are ".. tostring(plrsInGame))
	if plrsInGame < 2 then
		enoughPlayers = false
		gameStatusModule.notEnoughPlayers("Need at least 1 more player to start!", plrsInGame)
		if isEnoughPlayers then
			local enoughPlayers = true
		end
	end
end)

-- Module Script
local gameStatusModule = {}

local intermissionEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("IntermissionEvent")
local gameEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("GameEvent")
local postponeEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("PostponeEvent")

gameStatusModule.intermission = function(message, timer)
	local waitTime = 0
	local timer = 15
	while waitTime < 16 do
		intermissionEvent:FireAllClients(message, timer)
		warn(waitTime)
		waitTime += 1
		timer -= 1
		wait(1)
	end
	return true -- when waitTime hits 16, the function will return true when finished
end

gameStatusModule.startGame = function(message, timer)
	local waitTime = 0
	local timer = 10
	while waitTime < 11 do
		gameEvent:FireAllClients(message, timer)
		warn(waitTime)
		waitTime += 1
		timer -= 1
		wait(1)
	end
	return true
end

gameStatusModule.notEnoughPlayers = function(message, plrsInGame)
	while plrsInGame < 2 do
		postponeEvent:FireAllClients(message)
		wait(1)
	end
	return true
end

gameStatusModule.notEnoughPlayers2 = function(message, plrsInGame)
	postponeEvent:FireAllClients(message)
end

return gameStatusModule

--On Client
local intermissionEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("IntermissionEvent")
local gameEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("GameEvent")
local postponeEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("PostponeEvent")

local roundLabel = script.Parent

intermissionEvent.OnClientEvent:Connect(function(message, timer)
	roundLabel.Text = message..tostring(timer)
end)

gameEvent.OnClientEvent:Connect(function(message, timer)
	roundLabel.Text = message..tostring(timer).." seconds remaining"
end)

postponeEvent.OnClientEvent:Connect(function(message)
	roundLabel.Text = message
end)


You add an if statement to check that like this:

local Players = game:GetService("Players")

if #Players:GetPlayers <= 2 then
    --code goes here
end

well yes thats a simple way, but the problem is the gui. For example when a player leaves and there is one player, it runs the playerremoving script but it doesn’t override the intermission text.

here is the script i guess more simpler, the video above is the problem. I’m guessing it has something to do with the playeradded event. I’m trying to make it so that if a player leaves mid game… then it will automatically run the player removing event and stop everything in characteradded.


local plrsInGame = 0

local isEnoughPlayers = gameStatusModule.notEnoughPlayers

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if #Players:GetPlayers() >= 2 then
			warn("run intermission")
			gameStatusModule.intermission("Intermission: ", plrsInGame)
			local isIntermissionEnd = gameStatusModule.intermission
			if isIntermissionEnd then -- if returns true (when function is finished it will begin the next line of code)
				warn("run game")
				gameStatusModule.startGame("Drawer has ")
				local isGameEnd = gameStatusModule.startGame
				if gameStatusModule then
					warn("game ended")
					wait(1)
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	if #Players:GetPlayers() < 2 then
		gameStatusModule.notEnoughPlayers("Need at least 1 more player to start!", plrsInGame)
	end
end)

I suggest wrapping your code in a while task.wait() do loop, that way you don’t need the events and things will be kept more updated.

Awesome, thank you. A while loop did help but I also used break to stop the intermission and startgame module whenever it was < 2

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.