[LUA] timer.simple not working?

I’m trying to create a timer.simple for my BHOP Code. It won’t work tho. Anybody knows what’s wrong?



hook.Add("Think","autohop", function()

    if LocalPlayer():IsOnGround() then
        RunConsoleCommand("+jump")
        timer.simple(0.2, function() RunConsoleCommand("-jump") end )
    end
	
end)




timer.Simple(0.2, function() INSIDE SHIT end )


Isn’t that what I basicly did?

you have to capitalize simple since lua variables are case sensitive


timer.Simple(1.5, function()
print("Timer Simple is working")
end)

You should use CreateMove for bunnyhop.

If I’m not wrong, you can’t use a timer in a Think hook.

Well you can but its not correct.
Its a very bad practice because you can create 100 timers in 1 second, and that actually can be very bad.

It’s not incorrect, as long as you know the flow of your code it’s completely fine. In this case however it’s very bad and as stated above it should be done within a SetupMove hook.

Creating that many timers is bad. It will eventually overload the LinkedList or whatever data structure timers are held in.

I was thinking you couldn’t because it would be very inefficient, but as bigdogmat said, use a SetupMove hook if you’re trying to do a bunny hop script.

I believe you misunderstood, speaking from a stance of knowing that code isn’t going to run every think, e.g. having a system such as



local state

hook.Add("Think", "random", function()
  if state then
    state = false
    timer.Simple(0.2, function() end)
  end
end)

-- Code elsewhere that could edit state


Edit: Also, I’d have to guess it wouldn’t overload the structure it’s being held in unless the timer was quite long and you were creating many at a time.

For Bhop use this simple code.



if CLIENT then
	CreateClientConVar( "bhop_autojump", 1, true, true )
end
local band = bit.band
local bor = bit.bor
local bnot = bit.bnot
hook.Add("SetupMove", "_AutoJump", function(ply, uc)
	if CLIENT and GetConVarNumber( "bhop_autojump" ) ~= 1 then return end
	if SERVER and ply:GetInfoNum( "bhop_autojump", 1 ) ~= 1 then return end
	
	if ply:Alive() and ply:GetMoveType() == MOVETYPE_WALK and not ply:InVehicle() and ply:WaterLevel() <= 1 and band(uc:GetButtons(), IN_JUMP) == IN_JUMP then
		if ply:IsOnGround() then
			uc:SetButtons( bor( uc:GetButtons(), IN_JUMP) )
			local divide = 15
			local vel = ply:GetVelocity()/divide
			ply:SetVelocity(vel) 
		else
			uc:SetButtons( band( uc:GetButtons(), bnot(IN_JUMP)) )
		end
	end
end)



You must put this into shared lua.

You know I gain speed without adding speed manually per script, right? It’s just about the auto jump which is server sided.

[editline]13th September 2016[/editline]

Ok, did that. ty

[editline]13th September 2016[/editline]

Thank you.

The original problem, since as no one seemed to explain it is that the order of execution isn’t what you think it is.

The hook will call +jump multiple times before -jump once, it will also after a total elapsed time of 0.2s end up calling both +jump and -jump in the same think causing it to not work at all. It will also create them faster than they elapse, which will eventually use all the resources available.

Then comment the velocity part thats all. You are trying to make a bhop script on the hardest way. which depends on timers and you need to validate on each timer to call function. And I see you are trying to use it on client side but not all server allows you to use cleint side scripts.