Running a function randomly when server start

Hi all,

I am trying to achieve running a function randomly every 400-600 seconds and then run again but at a different time ( see code ) but my method doesn’t seem to work:



hook.Add( "Initialize", "StartRobber", function()
	
	local randomTime = math.random( 400, 600 )
	
	timer.Create( "RobberTimer", randomTime, 0, function() 
		timer.Adjust( "RobberTimer", math.random( 400, 600 ) )
		SpawnRobber()
	end)
	
end)


Anyone know a better way that works?

Thanks,
Computer600

snip

-snip-

For different intervals obviously.

It’s only the first two will be “random” in your code because then the third will be at the same time as the second and etc. Timer.Adjust would have worked if he had just called it correctly:

[lua]
local function AdjustTimer()
timer.Adjust( “RobberTimer”, math.random(400, 600), 0, function() AdjustTimer() RobberTimer() end)
end

hook.Add( “Initialize”, “StartRobber”, function()
timer.Create(“RobberTimer”, math.random(400, 600), 0, function()
AdjustTimer()
SpawnRobber()
end)
end)[/lua]
That should cause a looping timer that calls itself, and because Timers are just a table it overwrites itself by name with a new delay every time it is called.

Oh right, thanks.

Cheers, I just had to separate the hook and make a separate function called RobberTimer since AdjustTimer calls it. After that works like a charm