Timers.. o.O

[lua]
timer.Create( “afk_move”, 15, 0, function()
RunConsoleCommand( “+left” )
timer.Create( “afk_move2”, 0.01, 0, function()
RunConsoleCommand( “-left” )
end )
end )
timer.Stop( “afk_move” )

CreateClientConVar( “afk”, 0, false, false )
if GetConVarNumber( “afk” ) == 1 and ConVarExists( “afk” ) then
timer.Start( “afk_move” )
elseif GetConVarNumber( “afk” ) == 0 then
timer.Stop( “afk_move” )
end
[/lua]

So pretty much what I’m trying to do is make an anti-afk script. Every 15 seconds it moves me a little bit. So I start out by creating a timer which will, every 15 seconds, run +left in console then make another timer that 0.01 seconds later will run -left in console. This way it only turns left a tiny bit then stops. So I then stop the timer and check if my convar is on. If it is on then it starts the timer. But if it’s not, then it should stop the timer. But it’s not stopping the timer when the convar is turned off…

Make sure that you destory timer “afk_move2” so that way it is not always running. Because when you stop timer “afk_move” then that means that you don’t run anything inside of it.

Again, I just skimmed through this, not exactly sure. I just thought this might help a bit.

Few things:

  1. I would use +moveleft because most afk scripts check player position, not their angle.
  2. Just use 2 concommands for start and stop, its cleaner and easier.
  3. To make it so you don’t move off of a platform or something just hop back and forth between moveleft and moveright (go go modulus check)

[lua]
if CLIENT then
local movemod = 1
timer.Create(“afk_move”, 15, 0, function()
movemod = movemod + 1
if movemod % 2 == 0 then
RunConsoleCommand("+moveleft")
timer.Create(“afk_move2”, 0.01, 0, function()
RunConsoleCommand("-moveleft")
end)
else
RunConsoleCommand("+moveright")
timer.Create(“afk_move2”, 0.01, 0, function()
RunConsoleCommand("-moveright")
end)
end
end)
concommand.Add(“startafk”, function(ply,cmd,args)
timer.Start(“afk_move”)
end)
concommand.Add(“stopafk”, function(ply, cmd, args)
timer.Stop(“afk_move”)
end)
end
[/lua]

Code is tested and working.

Thanks for the code above, that works a lot better. I spent like an hour trying to find out how to make it move back to the right after it goes left… Not good with timers. Anyways, can you explain the

[lua]
movemod = movemod + 1
if movemod % 2 == 0 then
RunConsoleCommand("+moveleft")
timer.Create(“afk_move2”, 0.01, 0, function()
RunConsoleCommand("-moveleft")
end)
else
RunConsoleCommand("+moveright")
timer.Create(“afk_move2”, 0.01, 0, function()
RunConsoleCommand("-moveright")
end)
end
[/lua]

More specifically, the if movemod % 2 == 0 …

% is the lua operator for modulus (modular division) it returns the remainder of division.

Since “movemod” increases by 1 each time it’s updated, movemod % 2 will return either 0 or 1
3 % 2 = 1, because there is a remainder of 1 after you divide 3 by 2 (more so that 2 goes into 3 1 times, and after doing so there is a difference of 1)
3 % 3 = 0, because there is no remainder.
It’s kind of a tricky concept to think of for some, but once you understand it its really helpful.

Also put in the stopafk concommand a call for timer.Stop(“afk_move2”) - I just noticed that if you leave that running then you can’t move left because it calls -moveleft all the damn time.