Run a console command every x second.


I’m currently in the progress of creating my own gamemode, and I would like to know how I can make a RCON Console Command be ran every x second.

Maybe by a timer?


Yes, with a timer.

[lua]timer.Create( “Command”, 5, 0, function()
RunConsoleCommand( “say”, “Hello everyone!” )
end )[/lua]

And don’t sign your posts, we can see your username above your avatar.

How do I make the timer “restart”?
A console command every fifth minute, and another one every 25th minute.

I need a total of three console commands be ran at different times, but they shouldn’t just be ran and then stop, they all should like, “hello everyone” every fifth minute.

And how do I make a random timer? Between every 5-20 minute I want another command.


timer.Create( “Command”, math.random(5000,20000), 0, function()
RunConsoleCommand( “mp_restartgame” )
end )

Thank you very much, I’ll try it.
Should this be in gamemode/init.lua?
Or where?

Can be yea

For a console command every 5 minutes and one every 25 minutes:

[lua]timer.Create( “Command1”, 5 * 60, 0, function()
RunConsoleCommand( “say”, “5 minutes have passed!” )
end )

timer.Create( “Command2”, 25 * 60, 0, function()
RunConsoleCommand( “say”, “25 minutes have passed!” )
end )[/lua]

As you can see on [**the wiki page

**]( the third argument specifies how many times the timer runs. When you pass 0, it runs forever.

For a random timer, do something like this:

[lua]local nextRun

hook.Add( “Think”, “RandomTimer”, function()
if ( !nextRun or CurTime() > nextRun ) then
_G “function” … math.random( 1, 2 )

  nextRun = CurTime() + math.random( 5, 20 ) * 60

end )[/lua]

Where should I put this?
I put the both timers in my gamemode/init.lua, but nothing happens.

Did you wait 5 minutes?

I’ve been waiting since you posted that. Tried it instantly.
And I actually changed it from 5*60 to 180 (180 seconds it is?).

Add the following right above or right below the timer code to see if it’s running:
[lua]print( "

" )[/lua]

Nope, doesn’t print it.

then search your console for errors and post them.

No errors at all.

Put it in a function?