i need like if player.GetAll() == 0 then
MY CODE
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
[i]player.GetAll()[/i] 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 [i]#player.GetAll()[/i]
[QUOTE=dingusnin;44972685][i]player.GetAll()[/i] 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 [i]#player.GetAll()[/i][/QUOTE]
Might as well spoon-feed a little bit.
[Code]
function NumberOfPlayers()
return #player.GetAll()
end
[/Code]
[CODE]
if ( #player.GetAll() ) == 0 then
print( "no players online" )
end
[/CODE]
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() .. "\n" )
end)
end
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 :)
[QUOTE=code_gs;44975879]You need to put it into a function. Probably a think or a player leave hook for what you're trying to do.[/QUOTE]
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
[CODE]
timer.Create("timercheckplayerslolznoob",30,0,function()
if #player.GetAll() == 0 then
game.ConsoleCommand( "changelevel " .. game.GetMap() .. "\n" )
end
end)
[/CODE]
checks every 30 seconds if players are online, put in some file located at lua/autorun/server
[QUOTE=CertifiedCode;44975885]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[/QUOTE]
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.
[editline]whenever[/editline]
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: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/game_event_listeners.lua.html[/url]
player_connect and player_disconnect. On player connect, +1, on disconnect -1. O( 1 ) way to manage count.
[QUOTE=Acecool;44976107]Why even do that? You can manage the player-count without actually counting.
Use game-events: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/game_event_listeners.lua.html[/url]
player_connect and player_disconnect. On player connect, +1, on disconnect -1. O( 1 ) way to manage count.[/QUOTE]
What if no one connects or disconnects? How do you initialize it on server start as well?
[editline]2nd June 2014[/editline]
[QUOTE=JayneHJKL;44976073]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.
[editline]whenever[/editline]
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.[/QUOTE]
Are you okay? Sorry if this is a "dump bump" but using an infinite loop is DEFINATELY not the best way to do this.
[QUOTE=code_gs;44977451]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.[/QUOTE]
Simple; at the top of the script do: PLAYER_COUNT = PLAYER_COUNT || 0; and that is all...
[QUOTE=code_gs;44977451]What if no one connects or disconnects? How do you initialize it on server start as well?
.[/QUOTE]
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.
[QUOTE=Teddi Orange;44977509]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.[/QUOTE]
I wasn't aware of this; then PlayerInitialSpawn could be used to correct that issue.
[QUOTE=Acecool;44978118]I wasn't aware of this; then PlayerInitialSpawn could be used to correct that issue.[/QUOTE]
Or you know... just do it the correct way.
[QUOTE=Acecool;44976107]Why even do that? You can manage the player-count without actually counting.
Use game-events: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/game_event_listeners.lua.html[/url]
player_connect and player_disconnect. On player connect, +1, on disconnect -1. O( 1 ) way to manage count.[/QUOTE]
Why not make something 100 times harder for someone who is new at lua?
[QUOTE=nettsam;44984696]Why not make something 100 times harder for someone who is new at lua?[/QUOTE]
there isn't even a problem with using player.GetAll()
I don't see how this has gotten so complicated. All you'd need to do is hook into PlayerDisconnected, check if #Player.GetAll() is 0, wait 10 seconds and then restart if count is the same.
You can't just get the number of players from player.GetAll() that returns every players table.
You have to count the table, easy.
if table.Count(player.GetAll()) == 0 then
--blahblahstuffherelajsd;flkajsd;fasjd
end
# returns table count of a numerical indexed table, so #player.GetAll() is the same as table.Count( player.GetAll() )
Yeah, didn't see that post.
My bad.
I thought Lua scripts stopped loading when 0 players on server?
[QUOTE=AnonTakesOver;45015917]I thought Lua scripts stopped loading when 0 players on server?[/QUOTE]
Think hooks are not called, PlayerDisconnect is still called when the last player leaves.
if think hooks aren't called when play count is 0, doesn't that mean you couldn't use timers to wait ten seconds after someone leaves?
[QUOTE=MeepDarknessM;45016187]if think hooks aren't called when play count is 0, doesn't that mean you couldn't use timers to wait ten seconds after someone leaves?[/QUOTE]
I can't answer that, this was the case with old Lua timers, but since timers are now in engine, someone reported that they actually work if created when there are players and will work after there are no players.
Think hooks and timers each only work once a client has been connected to a server and will continue to work forever from what I can tell.
This includes bots.
[QUOTE=Willox;45016567]Think hooks and timers each only work once a client has been connected to a server and will continue to work forever from what I can tell.
This includes bots.[/QUOTE]
This is true. I connect a bot to my servers after they start up and kick them as soon as they are connected. This way, I can run my sockets module (it requires Think) when the servers are empty.
Sorry, you need to Log In to post a reply to this thread.