Generally, infinite loops (while true) are used incorrectly. Most of the time you want an exit condition, but that is a bit complicated to comprehend at the moment, so just do not use them yet. You simply didn’t think it through enough! And we don’t really need the repeat loop either since… well we only need to change the value of the text label whenever a change prompts it.
This is where logical creativity comes in. Fundamentally, you’re saying "whenever there are not enough players, we want the text label to say “not enough players!!”. In this scenario, we need two things. A trigger and a check. A trigger for when we should change the text and a check to see how many players we currently have.
I’ll now write pseudo code (…code that abstractly shows the concept but it is not real coding) to explicitly explain how it should look.
trigger.Connect(function()
checkhowManyPlayers()
end)
Alright! So, let’s dive into some real coding. Ultimately, it’s about players, so whenever a new player joins we check again, how many players do we NOW have. A useful event is called PlayerAdded, whenever a player joins, this event fires.
game.Players.PlayerAdded:Connect(function()
checkhowManyPlayers() -- this is still pseudo
end)
Alright, what can we do to get how many players we have! If you’re familiar with tables, maybe you didn’t know that you could get the size of this table by putting a # in front of it. For example #table → for ex: 4.
ex:
local table = {
1,
2,
3,
"apple",
}
We can see that the table has 4 values, thus when we do #table, it will give us 4 because there is 4 values in it. And as many knows, game.Players:GetPlayers() returns a table of all the players. Thus…
local players = game:GetService("Players")
players.PlayerAdded:Connect(function()
print(#players:GetPlayers()) --> how many players there are in the server
end)
Okay, so now we have a trigger for whenever we should check the playerlist. And since we want minimum two players, whenever a player is added, we will check if the serverpool is greater than 2.
local players = game:GetService("Players")
players.PlayerAdded:Connect(function()
if #players:GetPlayers() > 2 then
-- do something!
end
end)
Onto the big finale, we want to make our intermission counter and, we do indeed use a for loop to increase the counter. But we also want to have the authority to cancel this loop if it occurs that some player chooses to leave and we no longer have 2 players. A common stratergy is probably to implement a boolean check inside the for loop, I am unsure which is more performant but I have chosen to use a runService connection as they are fairly optimised.
--notice how I explicitly put the connection as a value, this is so that we can use the Disconnect method later on!
local debounce = false
local players = game:GetService("Players")
local connection = game:GetService("RunService").Heartbeat:Connect(function()
if debounce then return end
if #players:GetPlayers() >= 2 then
debounce = true
end
for i = 1, 60, 1 do
Status.Value = "Intermission: "..i
wait(1)
end
end)
Now make this new connection into a function and mesh it into with the code we learned since before.
local debounce = false
local players = game:GetService("Players")
local function intermission() -- made this into a function so that we can choose when to invoke it
local connection = game:GetService("RunService").Heartbeat:Connect(function()
if debounce then return end
if #players:GetPlayers() >= 2 then
debounce = true
end
for i = 1, 60, 1 do
Status.Value = "Intermission: "..i
wait(1)
end
end)
end
players.PlayerAdded:Connect(function()
intermission()
end)
And the more advanced API, playerRemoving, whenever the player leaves the game, we want to check whether we still have more than 2 players.
local function PlayerRemoving()
if players:GetPlayers() <= 2 then
debounce = false
cons:Disconnect()
cons = nil
end
end
The end reuslt will be this.
local debounce = false
local players = game:GetService("Players")
local function intermission() -- made this into a function so that we can choose when to invoke it
local connection = game:GetService("RunService").Heartbeat:Connect(function()
if debounce then return end
if #players:GetPlayers() >= 2 then
debounce = true
end
for i = 1, 60, 1 do
Status.Value = "Intermission: "..i
wait(1)
end
end)
end
local function PlayerRemoving()
if players:GetPlayers() <= 2 then
debounce = false
cons:Disconnect()
cons = nil
end
end
players.PlayerAdded:Connect(function(player)
intermission()
player.PlayerRemoving:Connect(playerRemoving)
end)