Jump to content
 Share

Roy

Change Map Every 24 Hours If Player Count Is < 1 Player

Recommended Posts

There's a Source Engine bug where if a map doesn't get changed after a while, the server will eventually become laggy/glitchy/(jittery?). This happens in every Source Engine game I know of. I noticed DarkRP is having this issue right now when playing on the server. 

 

I'd suggest making an addon that creates a timer that executes daily and restarts the map if the player count is less than one. Now, if this timer executes and the player count is less than one two - three times in a row (so it doesn't restart), I'd suggest forcing it to restart and giving warnings beforehand. This shouldn't be too hard to make and I may even be able to make it in the next couple of weeks when I have the time.

 

I haven't programmed in lua in a while, but something like the following should work excluding the warnings (I wrote it up pretty quickly and didn't test, so I apologize if it doesn't work right off the bat), but I'd suggest creating timers for warnings as well (e.g. 30 seconds before a forceful restart).

 

--[[ This should be placed inside of autorun/server since it's meant to be a server-side script. Please put it in addons/ as well for organization purposes. This was written real quick by Roy (Christian Deacon) and may contain errors and is not tested. But should work if no syntax errors. ]]--

-- Count that'll be incremented.
local count = 0

-- Maximum count before forcefully restarting map
local maxcount = 3

-- Create timer that executes every 86400 seconds (seconds in a day) repeatedly.
timer.Create("MapRestart", 86400, 0, function()
    -- Get player count.
    local players
    
    -- Loop through all players and add onto player count.
    for _, v in ipairs(player.GetAll()) do
      	-- Ensure player isn't bot, if so, continue.
      	if v:IsBot() then
        	goto ignore -- Apparently using "continue" doesn't work great according to -> https://wiki.facepunch.com/gmod/Specific_Operators
       	end
      
      	-- Increment player count.
    	players = players + 1 
      
      	::ignore::
    end
    
    -- Check if players is below 0 (server is empty) or if we've went above the maximum configured count.
    if players < 1 or count >= maxcount then
      -- Get map name.
      local map = game.GetMap()
      
      -- In case the script isn't reloaded on map change, set count to 0. I believe it should be reloaded on map change though.
      count = 0
      
      -- Restart map.
      RunConsoleCommand("changelevel", map)
      
     else
     	-- Increment count.
      	count = count + 1
     end
end)

 

I hope the above helps!

Share this post


Link to post
Share on other sites


Yea. The FPS drops were insane, I would get stuck at 30 FPS and it would make shooting sometimes unbearable when it would drop below 30 FPS. This would help dramatically and would probably make the player base rise up.

Share this post


Link to post
Share on other sites


On 11/11/2020 at 11:37 PM, Alexis said:

Yea. The FPS drops were insane, I would get stuck at 30 FPS and it would make shooting sometimes unbearable when it would drop below 30 FPS. This would help dramatically and would probably make the player base rise up.

Sadly I don't think this would improve your client-side FPS in-game :( 

 

Client-side optimization needs to go into the server for that (e.g. optimizations with the map such as using area portals correctly if they aren't already and optimizing GUIs, etc). Though, I also need to know your computer's specs.

 

This is just a bug that makes movements in-game feel really jittery, etc. 

Share this post


Link to post
Share on other sites


1 hour ago, Aurora said:

local players = #player.GetHumans()

https://wiki.facepunch.com/gmod/player.GetCount

On 11/11/2020 at 8:34 PM, Roy said:

There's a Source Engine bug where if a map doesn't get changed after a while, the server will eventually become laggy/glitchy/(jittery?). This happens in every Source Engine game I know of. I noticed DarkRP is having this issue right now when playing on the server. 

 

I'd suggest making an addon that creates a timer that executes daily and restarts the map if the player count is less than one. Now, if this timer executes and the player count is less than one two - three times in a row (so it doesn't restart), I'd suggest forcing it to restart and giving warnings beforehand. This shouldn't be too hard to make and I may even be able to make it in the next couple of weeks when I have the time.

 

I haven't programmed in lua in a while, but something like the following should work excluding the warnings (I wrote it up pretty quickly and didn't test, so I apologize if it doesn't work right off the bat), but I'd suggest creating timers for warnings as well (e.g. 30 seconds before a forceful restart).

 

--[[ This should be placed inside of autorun/server since it's meant to be a server-side script. Please put it in addons/ as well for organization purposes. This was written real quick by Roy (Christian Deacon) and may contain errors and is not tested. But should work if no syntax errors. ]]--

-- Count that'll be incremented.
local count = 0

-- Maximum count before forcefully restarting map
local maxcount = 3

local map = game.GetMap()
      
-- Create timer that executes every 86400 seconds (seconds in a day) repeatedly.
timer.Create("MapRestart", 86400, 0, function()
    -- Get player count.
    local players = player.GetCount()
    
    -- Check if players is below 0 (server is empty) or if we've went above the maximum configured count.
    if players < 1 or count >= maxcount then
      -- In case the script isn't reloaded on map change, set count to 0. I believe it should be reloaded on map change though.
      count = 0
      -- Restart map.
      RunConsoleCommand("changelevel", map)
      
     else
     	-- Increment count.
      	count = count + 1
     end
end)

 

I hope the above helps!

 


ced6b12d-b149-44f6-bc2b-a0806b5a22bf.png rsnJ.gif

Share this post


Link to post
Share on other sites


I looked for a function like that and couldn't find any, but I guess I missed those haha. @Yogpod pointed that out the other night. My script should still work, but yeah can be simplified by using player.GetCount() or w/e.

Share this post


Link to post
Share on other sites


18 minutes ago, Yogpod said:

player.GetCount includes bots and Roy explicitly wanted those excluded judging by his original code.

 

On second thought, it doesn't matter since there has to be at least one human player for bots to exist.


71CFA5EE-923C-4740-ACF4-508B753C9AD7.png.92a0d40c0fa7773f71fea2453f581d16.png

(signature made by @Kaylode)

Previously known as Xy.

 

Twitter ❤️Ko-Fi ❤️Github

 

Screenshot from 2021-09-23 07-43-42.png IMG_0248.jpg

 

ben_mixed_opinions.png

Share this post


Link to post
Share on other sites


6 hours ago, Aurora said:

player.GetCount includes bots and Roy explicitly wanted those excluded judging by his original code.

 

On second thought, it doesn't matter since there has to be at least one human player for bots to exist.

simplac bot might mess that up, may as well just do #player.GetHumans


ced6b12d-b149-44f6-bc2b-a0806b5a22bf.png rsnJ.gif

Share this post


Link to post
Share on other sites


 

33 minutes ago, Yogpod said:

simplac bot might mess that up, may as well just do #player.GetHumans

 

The player.GetHumans() is great but the bot doesn't connect until 2 or 3 players are on the server. 


1654039214_lennyfacesignaturejarm2_webp.png.77166eade8f7320848ec595e26aa1230.png

 

Share this post


Link to post
Share on other sites




×
×
  • Create New...