Shutdown-Script for Don't Starve Together Dedicated Server I have written my first LUA-Script/Function to shut down the Dedicated Server for Don't Starve Together.
I wanted to shut down the server in 2 minutes and inform everyone on the server that the server is shutting down.
The function os.execute is nil in the dedicated server console, so I can't use os.execute("sleep " .. seconds).

Can I improve something on this script? Is it best practice?

function sleep(seconds)
    local t = os.time()
    local diff = 0
    while diff &lt; seconds do
        diff = os.difftime(os.time(), t)
    end
end

local steps = {120,60,30,10,5,4,3,2,1}
local n = table.getn(steps)
for i=1,n-1,1 do
  TheNet:SystemMessage("The server shuts down in " .. steps[i] .. " seconds")
  sleep(steps[i] - steps[i+1])
end
TheNet:SystemMessage("The server shuts down in " .. steps[n] .. " seconds")
sleep(steps[n])
TheNet:SystemMessage("The server is shutting down now")
c_shutdown(true)


Link to the server command list for Don't Starve.


TheNet:SystemMessage prints the message into the chat
c_shutdown(true) shutdown the server and persist the world
table.getn has been deprecated for more than 2 years now.
Why iterate only until n-1?
I'd prefer using the string.format over plain concatenation. This is just a personal preference, as the strings are easier to read that way.
Although you do not need to pass true to the c_shutdown, do mention in a comment what it does. Maintaining comments is a nice way to recognise engine specific hard-bound parameters; without having to refer the docs again.
Iterate over the table using ipairs


Rewritten, the code would be:

local function sleep(seconds)
    local diff, t = 0, os.time()
    while diff &lt; seconds do
        diff = os.difftime(os.time(), t)
    end
end

local steps = {
  120, 60, 30,
  10, 5, 4, 3,
  2, 1
}
local notice = "The server shuts down in %d seconds"

for _, secs in ipairs(steps) do
  TheNet:SystemMessage(notice:format(secs))
  sleep(secs)
end
TheNet:SystemMessage("The server is shutting down now")
c_shutdown(true)  -- true parameter implies that the game will be saved