Binding function to key

Hi!
Currently I’m working on my own gamemode and I’m wanting to add super jump but I don’t want to overwrite the default jump but I want it blinded to space when you double press, if that makes sense, any help would be awesome!

And on another note Im adding an energy system and I would like to know if I could change a variable like this



local MyVar = 100
MyVar = MyVar - 10

Or would I have to use a math clamp

[editline]n[/editline]

As for the approach question, try to play around with this;

[lua]local variable = 0
local speed = 200

local goal = 720

hook.Add(“HUDPaint”, “”, function()
surface.SetDrawColor(255, 255, 255)
surface.DrawRect( 0, 0, variable, 128 )

variable = math.Approach( variable, goal, speed * FrameTime() )

end)[/lua]

Slowly makes the rectangle grow from 0 (variable) to 720 (goal) in a speed of 200 (speed)

[lua]hook.Add(“Think”, “flying”, function()
if(input.IsKeyDown(KEY_SPACE)) then
net.Start(“superjump!”)
net.SendToServer()
end
end)[/lua]

Thanks, quickest response I’ve had before

[editline]6th March 2015[/editline]

So would this work


local time = 0.34 -- X second delay
local amount = 250 -- X amount of velocity in jump
local energuse = 10

if SERVER then
	util.AddNetworkString("superjump!")
	net.Receive("superjump!", function(length, client)
		if IsValid(client) then
			client:SetVelocity( Vector(0, 0, amount) )
                 client.energy = math.Approach( client.energy - energuse ) -- I have a global variable called energy
		end
	end)
else
	local SJ_PRESSED = false

	hook.Add("PlayerBindPress", "super jump", function( ply, bind, pressed )
		if (bind == "+jump") and pressed then
			if SJ_PRESSED then
				net.Start("superjump!")
				net.SendToServer()
				SJ_PRESSED = false
			else
				SJ_PRESSED = true

				timer.Simple( time, function()
					SJ_PRESSED = false
				end)
			end
		end
	end)
end

don’t use his code for double jumping, it’s super exploitable (what I posted is one way to do it)

[editline]5th March 2015[/editline]

here’s something from an old abandoned project of mine

[lua]function GM:KeyPress(ply, key)
if(key == IN_JUMP) then
if not(ply:IsOnGround() or ply.DoubleJumped) then
ply.DoubleJumped = true

		ply:SetLocalVelocity(Vector(ply:GetVelocity().x, ply:GetVelocity().y, 300))
	end
end

end

function GM:OnPlayerHitGround(ply)
ply.DoubleJumped = false
end[/lua]

Ok thanks so if I wanted it to deplete energy it would be like this ( I have a global player variable)



local energuse = 10 -- amount of energy been used

function GM:KeyPress(ply, key)
	if(key == IN_JUMP) then
		if not(ply:IsOnGround() or ply.DoubleJumped) then
			ply.DoubleJumped = true
                        if ply.energy < energuse then
                        ply:PrintMessage(HUD_PRINTTALK, "You Need More Energy")
else

			ply:SetLocalVelocity(Vector(ply:GetVelocity().x, ply:GetVelocity().y, 300))
-- going to add sound soon
                        ply.energy = math.Approach( ply.energy - energuse )
		end
	end
end

function GM:OnPlayerHitGround(ply)
-- going to add sound soon
	ply.DoubleJumped = false
end

Then in another file I would have



ply.energy = 30 -- my global variable

local speed = 200

local goal = 30

 function energyregain ( ply )
        if ply.energy == 30 then
        ply.MsgN("No Energy Regain Needed")
else
	variable = math.Approach( ply.energy, goal, speed * FrameTime() )
  -- will add sound
end)


 

Just something quick done from my IPad

[editline]6th March 2015[/editline]

Another quick question, would this be server side of client side or even shared?