Help with timers.

Hi, I’m trying to make it so that when maintenanceon == true, every 30 seconds print a message (serverside) until maintenanceon == false.

Here is my full code as of current:


 --Startup--
local name = "Maintenance script"
local author = "DJMullaney"
local version = "1.0"

print("
")
print(name .. " - " .. author .. ".")
print("Version: " .. version)
print("
")

--Variables--
local hostname_maintenance = "[DJ] Servers | Maintenance |"
local hostname_normal = (GetConVarString("hostname"));
maintenanceon = false

--Start of code--
CreateConVar("sv_maintenance", "0", true, false, "Enable maintenance mode.")


cvars.AddChangeCallback("sv_maintenance", function(_, _, newval)
   newval = tonumber(newval)
   
   if (newval) then -- Valid number
      if (not enabled and newval > 0) then -- Changed from false to true
        enabled = true
        -- Enabled code here
		RunConsoleCommand("hostname", hostname_maintenance)
		chatWarning()
		maintenanceon = true
      elseif (enabled and newval == 0) then
        enabled = false
        -- Disabled code here
		RunConsoleCommand("hostname", hostname_normal)
		maintenanceon = false
		PrintMessage(HUD_PRINTTALK, "Maintenance mode: Off")
      end
   end
end)


function chatWarning()
	print("ChatWarning() called.")
	PrintMessage(HUD_PRINTTALK, "Maintenance mode: On")
	PrintMessage(HUD_PRINTTALK, "The server may become unstable or close unexpectedly!")
end


if maintenanceon == true then
	timer.Create("WarningMessage", 30, 0, chatWarning)
end 

The code is placed in lua/autorun.

This should work:


 --Startup--
local name = "Maintenance script"
local author = "DJMullaney"
local version = "1.0"

print("
")
print(name .. " - " .. author .. ".")
print("Version: " .. version)
print("
")

--Start of code--
local hostname_normal = GetConVarString("hostname")
local hostname_maintenance = hostname_normal .. " | Maintenance |"
local maintenanceon = false

local function chatWarning()
	PrintMessage(HUD_PRINTTALK, "Maintenance mode: On")
	PrintMessage(HUD_PRINTTALK, "The server may become unstable or close unexpectedly!")
end

CreateConVar("sv_maintenance", "0", 0, "Enable maintenance mode.")

cvars.AddChangeCallback("sv_maintenance", function(_, _, newval)
   newval = tonumber(newval)
   
   if (newval) then -- Valid number
      if (not maintenanceon and newval > 0) then -- Changed from false to true
        enabled = true
        -- Enabled code here
		RunConsoleCommand("hostname", hostname_maintenance)
		maintenanceon = true
		timer.Create("WarningMessage", 30, 0, chatWarning)
      elseif (maintenanceon and newval == 0) then
        maintenanceon = false
        -- Disabled code here
		RunConsoleCommand("hostname", hostname_normal)
		maintenanceon = false
		PrintMessage(HUD_PRINTTALK, "Maintenance mode: Off")
		timer.Destroy("WarningMessage")
      end
   end
end)

I put the timer in the change hook and changed all of your globals to locals. Also, you had weird arguments into CreateConVar. Lastly, this should be in lua/autorun/server.

I’ll give it a shot, thanks!

Thanks, this works. For some reason though, when maintenance mode is turned off, the message is printed 6 times?

Here is the code:


  --Startup--
local name = "Maintenance script"
local author = "DJMullaney"
local version = "1.0"

print("
")
print(name .. " - " .. author .. ".")
print("Version: " .. version)
print("
")

--Start of code--
local hostname_normal = GetConVarString("hostname")
local hostname_maintenance = "[DJ] Servers | Maintenance |"
local maintenanceon = false

function chatWarning()
	PrintMessage(HUD_PRINTTALK, "Maintenance mode: On")
	PrintMessage(HUD_PRINTTALK, "The server may become unstable or close unexpectedly!")
end

CreateConVar("sv_maintenance", "0", 0, "Enable maintenance mode.")

cvars.AddChangeCallback("sv_maintenance", function(_, _, newval)
   newval = tonumber(newval)
   
   if (newval) then -- Valid number
      if (not maintenanceon and newval > 0) then -- Changed from false to true
        enabled = true
        -- Enabled code here
		RunConsoleCommand("hostname", hostname_maintenance)
		maintenanceon = true
		chatWarning()
		timer.Create("WarningMessage", 30, 0, chatWarning)
      elseif (maintenanceon and newval == 0) then
        maintenanceon = false
        -- Disabled code here
		timer.Destroy("WarningMessage")
		RunConsoleCommand("hostname", hostname_normal)
		maintenanceon = false
		PrintMessage(HUD_PRINTTALK, "Maintenance mode: Off")
      end
   end
end) 

Where is the script located? Also, change chatWarning to be a local function.

lua/autorun/server

I fixed it now :smile: Thanks for the help!