Ulysses Stuff > Suggestions

Auto-server reset script

<< < (2/3) > >>

jay209015:

--- Code: ---// ARestart V1.0

local RDelay = 5 // Delay in minutes

function Reset()
local playerslist = player.GetAll()
if #playerslist == 0 then
timer.Create("ResetTimer", RDelay * 60, 1, RestartServer)
else
if timer.IsTimer("ResetTimer") then
timer.Destroy("ResetTimer")
end
end
timer.Create("ResetThinkTimer", 60, 0, Reset)
function RestartServer()
game.ConsoleCommand( "quit" )
end
--- End code ---

Ok, added Megiddo's suggestion.


--- Quote ---table.Count() shouldn't be used, use the # operator instead.
--- End quote ---

Megiddo:
Let me revise my statement there. I thought you were actually using table.getn(), so it's time for a little history lesson!

# is a lua unary operator for getting the number of indexed values in a table. That means it will only count values that have a numeric index. It works perfectly for player.GetAll() and it's what you should use for that.

table.getn() basically maps to the # operator in lua 5.1. It's deprecated so you shouldn't use it.

table.Count() is another garry function that's nice but mistakenly used. Unlike the unary operator #, it counts ALL values in a table. You should only ever use it in cases where # won't since it's rather inefficient.

JerryTheStampede:
Jay do you mind if I put your code on Facepunch, I'd give you credit of course. I think that a lot more people would be interested in this.

Tophat Man:
Would there be a way to have the timer start when there is 1 player on and then they leave, so it doesn't keep resetting itself over and over when no one even went on?

Megiddo:
Timers aren't called until someone joins (or at least this used to be the behavior), so it shouldn't continually restart already.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version