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)