checking if 0 players online

i need like if player.GetAll() == 0 then


but im new to lua so im trying to check if theres 0 players online and i really need help i searched on google couldnt find one thing.

SO Check if 0 players online if theres 0 players then it will execute my code ill right below it. sorry im not english that much sorry if you dont understand me

player.GetAll() will return a table. To get the number of numerical keys in the table (in this case the number of players) then you would need to use #player.GetAll()

Might as well spoon-feed a little bit.

function NumberOfPlayers()
    return #player.GetAll()

if ( #player.GetAll() ) == 0 then
     print( "no players online" )

hello i need to put this in it this dosnt seem to work?

if ( #player.GetAll() ) == 0 then
timer.Create( “restart_ok”, 30, 0, function()
game.ConsoleCommand( "changelevel " … game.GetMap() … "
" )

You need to put it into a function. Probably a think or a player leave hook for what you’re trying to do.

sorry quite new to lua just wanted that simple code to work. i dont thing i have to add a hook for when the player leaves tho because im making it when 0 players online not when a specific player leaves sorry if im wrong as i said i am quite new

[editline]2nd June 2014[/editline]

hello i still need help its a simple thing tho but new to lua please help me if you can :slight_smile:

I heard Think isnt called if there’s no players on. Correct me if I am wrong

i think your right could you link me i know your right but dont know what page to find it on the wiki

   if #player.GetAll() == 0 then 
       game.ConsoleCommand( "changelevel " .. game.GetMap() .. "
" )

checks every 30 seconds if players are online, put in some file located at lua/autorun/server

hooks are not complicated, they are efficient.
Instead of looping over and over, taking precious CPU cycles you can wait for interesting events to happen.

But fuck it. just do an infinite loop anyway. It’s not like anyone cares about best practices or trying to be better.
Who even cares. Just half ass everything so you can make quicker progress.
And hate on everyone you can for sharing opinions or trying to inspire ideas thoughts and questions of their own.

Don’t forget to rate people dumb for sharing anything other than spoon fed code snippets.

Thankyou all so much love lua just getting to know its possibilities.

Why even do that? You can manage the player-count without actually counting.

Use game-events:

player_connect and player_disconnect. On player connect, +1, on disconnect -1. O( 1 ) way to manage count.

What if no one connects or disconnects? How do you initialize it on server start as well?

[editline]2nd June 2014[/editline]

Are you okay? Sorry if this is a “dump bump” but using an infinite loop is DEFINATELY not the best way to do this.

Simple; at the top of the script do: PLAYER_COUNT = PLAYER_COUNT || 0; and that is all…

Not only this but the player_connect event isn’t fired if the map changes level with players on it meaning you’d have players unaccounted for.

I wasn’t aware of this; then PlayerInitialSpawn could be used to correct that issue.

Or you know… just do it the correct way.

Why not make something 100 times harder for someone who is new at lua?

there isn’t even a problem with using player.GetAll()