It is tricky to detect when the server starts up without the help of external scripts. There are no hooks, variables or functions that tell us this kind of information.
The only thing I found that seems to persists across sessions is the connection ID. This ID starts at 2 and increments by 1 every time a player (or bot) joins the server. This ID resets when the server shuts down but not when the server changes maps.
This can be used to create a function that gets called every time the server starts its first session:
-- lua/ulx/modules/startup.lua
local function onFirstSession()
RunConsoleCommand( "ulx", "randommap" )
end
hook.Add( "Think", "check-first-session", function()
local bot = player.CreateNextBot( "Bot" )
if IsValid( bot ) then
hook.Add( ULib.HOOK_UCLAUTH, "auth-startup-bot", function()
if bot:UserID() == 2 then onFirstSession() end
bot:Kick()
hook.Remove( ULib.HOOK_UCLAUTH, "auth-startup-bot" )
end )
end
hook.Remove( "Think", "check-first-session" )
end )
Update: Much better and simpler approach:
-- lua/ulx/modules/startup.lua
hook.Add( ulx.HOOK_ULXDONELOADING, "random_map_on_first_session", function()
if game.GetGlobalCounter( "ulx_randommap" ) == 0 then
game.SetGlobalCounter( "ulx_randommap", 1 )
RunConsoleCommand( "ulx", "randommap" )
end
hook.Remove( "Think", "random_map_on_first_session" )
end )