I’m trying to write a script that checks if there is a certain amount of players in a table; this is the easy part. However, I also want to check constantly if the player count is less than a certain number. This is the difficult part.
I want to run a countdown function if, let’s say, there are 10 or more people in a table. If this number ever fluctuates to below ten, then I want to interrupt this function. And if there are 20 people or so, then I want to make it so another function runs which cancels the function.
If I need to explain further I’d be happy to. How would I solve this problem?
That’s useful. However, I have a preset table that is logging players that is separate from the player count. Some players in the server may not be a part of the table.
I think the best way to do this is to do an if statement to check the player count every time the countdown is being updated. You could return a boolean value from the function to trigger or not trigger whatever the countdown was for.
local function startCountdown(seconds)
for i = seconds,0,-1 do
if playerCount < 10 then
return false
end
timeLeft = i
wait(1)
end
return true
end
if startCountdown(5) == true then
--Start Whatever Was Needed
end
I’ve used if statements in the past numerous times and understand how to count the players, however I don’t understand how to interrupt the function if the player count dips below a certain number.
Yes , you can use return to return nothing if a criteria is not met , otherwise the opposite.
For example :
local count = #game:GetService("Players"):GetPlayers()
if not count >= 5 then return false end
print("this line runs only if the above criteria is met")
-- when there are 5 or more players
or maybe just return end simply.
This won’t work unless you constantly check for the players count as players could leave and join before or after we run the above code, to prevent run the above code connected to a Players.PlayerAdded and Players.PlayerRemoving event.
local requiredPlayers = 10
while true do
local players = #game:GetService("Players"):GetPlayers()
if players < requiredPlayers then
print("Not enough players")
elseif players >= requiredPlayers then
print("Enough players, running intermission")
end
wait()
end
good idea, but wrapping it in while loop would be a bit redundant, when you know that the only occurrences when the player count would change would be the events PlayerAdded and PlayerRemoved.
Don’t do this. These kinds of loops can create unnecessary latency.
@iy_uhn I would just use events like @XxELECTROFUSIONxX mentioned. For the sake of simplicity you can update a number value in ReplicatedStorage when someone joins or leaves, then have a separate event check if that value changes to determine what should be done.
If you meant as in a table, then try something like this :
local tab = {}
local function CheckIfLess (tab)
if #tab <= 10 then
return true -- meaning lesser than or = 10 elements
end
-- within your function
if CheckIfLess(tab) then
return error("lesser than 10")
end
-- continue with function
return can be used in this way :
num = 5
if num < 10 then return end
print("this will not print") -- because the above criteria returned false
end
What I have is this part that when it’s touched, it will add their username to a queue. There is also the option to remove yourself from the queue.
If you have not interacted with the queue then your name will not be in the table. This is why I don’t want all the players in the server affected by this table.