I'm really new to lua and am trying to make a plugin for my server but can't find out how to check if the server has bean empty for X amount of time, its pro something simple but i just can't find it.
type of code I'm trying to make
[CODE]if (server.empty for X amount of time) then
RunConsoleCommand(bla bla bla)
end[/CODE]
and
[CODE]if (player joined) then
RunConsoleCommand(bla bla bla)
end[/CODE]
This is not possible due to the fact that if server is empty, it stops all Think hooks.
humm ok well would it be possible to run code as the last player is leaving
He never said he was using Think hooks.
Here is a crude script that has the basic functionality of what you might need:
[CODE]
ServerEmptyTime = ServerEmptyTime or 0
hook.Add( "PlayerDisconnected", "StartEmptyTime", function( ply )
if ( #player.GetAll() <= 1 ) then
timer.Create( "ServerEmpty", 1, 0, function()
ServerEmptyTime = ServerEmptyTime + 1
end )
end
end )
hook.Add( "PlayerInitialSpawn", "ClearEmptyTime", function( ply )
timer.Destroy( "ServerEmpty" )
ServerEmptyTime = 0
RunConsoleCommand( "" )
end )
[/CODE]
[B]PlayerDisconnected[/B] (called when a player disconnects) -- Our function checks if the current amount of players is less than/equal to 1 (because it will still return 1 when the last player disconnects since they haven't actually disconnected yet).
It creates a timer that runs every second, increasing some global value you can use elsewhere. You can change it to be more precise if you want, like 0.1.
[B]PlayerInitialSpawn [/B](called when a player loads in the server) -- The timer we created will be destroyed (we don't need to check it with timer.Exists first since calling timer.Destory on a timer that doesn't exist will just be ignored).
You can use RunConsoleCommand inside that same hook when a player joins.
[B]As others have pointed out[/B] -- this won't be able to tell you how long the server has been empty until at least one player has connected because it relies on PlayerDisconnected to create the timer. If you try creating a timer before any players have connected, it won't run.
However, if you're wanting to do something like clean up a disconnected player's props after X time, this would work fine since the timer will only be created when a player has joined and disconnected.
Again, crude example but it might work for what you want.
[QUOTE=Mista Tea;44533801]He never said he was using Think hooks.
Here is a crude script that has the basic functionality of what you might need:
[CODE]
ServerEmptyTime = ServerEmptyTime or 0
hook.Add( "PlayerDisconnected", "StartEmptyTime", function( ply )
if ( #player.GetAll() <= 1 ) then
timer.Create( "ServerEmpty", 1, 0, function()
ServerEmptyTime = ServerEmptyTime + 1
end )
end
end )
hook.Add( "PlayerInitialSpawn", "ClearEmptyTime", function( ply )
timer.Destroy( "ServerEmpty" )
ServerEmptyTime = 0
RunConsoleCommand( "" )
end )
[/CODE]
Essentially, when a player disconnects, it checks if the current amount of players is less than/equal to 1 (because it will still return 1 when the last player disconnects since they haven't actually disconnected yet).
It creates a timer that runs every second, increasing some global value you can use elsewhere. You can change it to be more precise if you want, like 0.1.
When a player joins, the timer will be destroyed (no real need to check if it exists, since it just wouldn't do anything).
You can use RunConsoleCommand inside that same hook when a player joins.
Again, crude example but it might work for what you want.[/QUOTE]
Yes, but timers won't run when server is empty either. You can only execute something just before server goes empty, that's true.
I just tested this on my own server and disconnected (no players connected) and it ran fine.
[CODE]lua_run print(ServerEmptyTime)
> print(ServerEmptyTime)...
11
> print(ServerEmptyTime)...
17
lua_run print(ServerEmptyTime)
> print(ServerEmptyTime)...
18
lua_run print(ServerEmptyTime)
> print(ServerEmptyTime)...
20
lua_run print(ServerEmptyTime)
> print(ServerEmptyTime)...
81
lua_run print(ServerEmptyTime)
> print(ServerEmptyTime)...
147
lua_run print(ServerEmptyTime)
> print(ServerEmptyTime)...
217
lua_run print(ServerEmptyTime)
> print(ServerEmptyTime)...
341
lua_run print(ServerEmptyTime)
> print(ServerEmptyTime)...
643[/CODE]
[QUOTE=Robotboy655;44533743]This is not possible due to the fact that if server is empty, it stops all Think hooks.[/QUOTE]
I was under the impression that if a player joins and leaves then that will wake up the server and think hooks will still run but if no one has ever joined during that map it will stay in its sleep state until a player or bot joins.
Timers won't start if the server is empty, but they will run fine and keep running if they are started before the server is empty.
i'm probably wrong here, but aren't timers in C now? would they still use the think hook?
if so you could always just do something with os.date or CurTime or something
[QUOTE=Robotboy655;44533743]This is not possible due to the fact that if server is empty, it stops all Think hooks.[/QUOTE]
But timers might work.
You can execute a timer hooked to PlayerDisconnected with check if it's the last person.
I wasn't aware that timers work without any players now, nice find.
Oh, sorry Mista Tea, I didn't read your post.
so will this make it so that the timers always work ?
[CODE]hook.Add( "PlayerDisconnected", "BotSpawn", function( ply )
if ( #player.GetAll() <= 1 ) then
RunConsoleCommand( "bot" )
end
end )
hook.Add( "PlayerInitialSpawn", "BotKill", function( ply )
RunConsoleCommand("kick Bot01")
RunConsoleCommand("ulx kick Bot01") -- <<< don't know what one to use
RunConsoleCommand("kickid BOT")
end )[/CODE]
Sorry, you need to Log In to post a reply to this thread.