Constantly check for player count

Hey everyone!

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?

1 Like

Do you mean a table of people online, or people that have their data saved for your game?

I have a system that logs players in a table based on name in game. I’m using it to track players in a queue system.

I think you can use a while loop:
`

 while #table > 10 then
    -- run code
 end

 function blablabla()
     -- run code`

Is that what you need?

to get the amount of players currently in a server use

count = #game:GetService("Players"):GetPlayers()

and connect to onPlayerAdded and OnPlayerRemoving to be much more efficient then constantly looping/

1 Like

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
2 Likes

Use if statements. For example

if playerCount < 10 then

then you will be able to run certain actions.

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.

old asset I made for this, really messy.

You can edit to do something once the count is below or above a specific number, and then run a function.

In that example I just prevent rounds from starting unless a specific number of player’s is reached.

I’ll take a look.
30 chars woahhh

You would break the function by return.

You can simply use return end. By this way the function stops whenever you call it

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.

1 Like

You could probably try doing something like this:

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
1 Like

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.

1 Like

I already have a script to count players in the queue. I do not need a script to count players in the server. Sorry for the confusion.

can we know what the server queue is again , as in your case?

Edit :

@iy_uhn

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

Sure.

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.