How can I make a double jump that works only for one class?

I have a double jump function (Thanks to Kilburn, I only changed it so it propels you forward), however, I’d like to know how I can make it work for only one class out of the four in my gamemode; (It’s based on Fretta)

An alternative is adding it to that class’ class exclusive SWEP, however, I tried this and it still somehow enabled the use of the boost jump in every other class.
Upon changing all the "pl"s to “self.Owner” the weapon would not spawn with that class.


hook.Add("KeyPress", "DoubleJump", function(pl, k)  
     if not pl or not pl:IsValid() or k~=2 then  
         return  
     end  
           
     if not pl.Jumps or pl:IsOnGround() then  
         pl.Jumps=0  
     end  
       
     if pl.Jumps==2 then return end  
       
     pl.Jumps = pl.Jumps + 1  
     if pl.Jumps==2 then  
         local ang = pl:GetAngles()  
         local forward, right = ang:Forward(), ang:Right()  
           
         local vel = -1 * pl:GetForward() -- Nullify current velocity  
         pl:SetVelocity(pl:GetForward() * 300) -- Add forward force  
		 pl:EmitSound("swing.wav")
           
         local spd = pl:GetMaxSpeed()  
           
         if pl:KeyDown(IN_FORWARD) then  
             vel = vel + forward * spd  
         elseif pl:KeyDown(IN_BACK) then  
             vel = vel - forward * spd  
         end  
           
         if pl:KeyDown(IN_MOVERIGHT) then  
             vel = vel + right * spd  
         elseif pl:KeyDown(IN_MOVELEFT) then  
             vel = vel - right * spd  
         end  
           
         pl:SetVelocity(vel)  
     end  
 end) 

The double jump isn’t technically a double jump, it’s meant to speed you up in midair.

If it’s fretta, why don’t you put the function into the player class?

It is Fretta, and I did put the function into the class_classname.lua. It affected all 4 classes, despite me only copying it into the lua of that one class.

Why are you doing this with a hook?

Somewhere where you’re defining the class you want to double jump put something similar to…

[lua]function CLASS:OnKeyPress( pl, key )
if( key != IN_JUMP ) then return end

–doublejump code goes here
end
[/lua]

Someone asked a similiar question, and Kilburn gave this script. I’m still learning Lua, but doing so by working on a gamemode.

Updated my post but you ninja’d me.

Thanks for the Lua. I’ll get back to you on it.

Yeah of course I’m coding my TF2 gamemode from the base gamemode, with my own class system, so it’s kinda obvious that you have to adapt it if you’re going to use it in Fretta.

[lua]function CLASS:OnKeyPress( pl, key )
if( key != IN_JUMP ) then return end
hook.Add(“KeyPress”, “DoubleJump”, function(pl, k)
if not pl or not pl:IsValid() or k~=2 then
return
end

 if not pl.Jumps or pl:IsOnGround() then  
     pl.Jumps=0  
 end  
   
 if pl.Jumps==2 then return end  
   
 pl.Jumps = pl.Jumps + 1  
 if pl.Jumps==2 then  
     local ang = pl:GetAngles()  
     local forward, right = ang:Forward(), ang:Right()  
       
     local vel = -1 * pl:GetForward() -- Nullify current velocity  
     pl:SetVelocity(pl:GetForward() * 30000000000000000000000000000) -- Add forward force  
	 pl:EmitSound("knuxswing.wav")
       
     local spd = pl:GetMaxSpeed()  
       
     if pl:KeyDown(IN_FORWARD) then  
         vel = vel + forward * spd  
     elseif pl:KeyDown(IN_BACK) then  
         vel = vel - forward * spd  
     end  
       
     if pl:KeyDown(IN_MOVERIGHT) then  
         vel = vel + right * spd  
     elseif pl:KeyDown(IN_MOVELEFT) then  
         vel = vel - right * spd  
     end  
       
     pl:SetVelocity(vel)  
 end  

end
–doublejump code goes here
end [/lua]

Did I put it in wrong? I’m pretty new to Lua, so I probably did something stupid.

Just had a look and gmdm does it like this…
[lua]
function CLASS:Move( pl, mv )

if( pl.DoubleJump == nil ) then pl.DoubleJump = false end;

if( !pl:IsOnGround() and pl:KeyPressed( IN_JUMP ) and !pl.DoubleJump ) then
	local vel = mv:GetVelocity();
	mv:SetVelocity( Vector( vel.x, vel.y, self.JumpPower * 1.75 ) );
	pl.DoubleJump = true;
elseif( pl:IsOnGround() and pl.DoubleJump ) then
	pl.DoubleJump = false;
end

end
[/lua]

Thank you. Total repair.

The function is complete, I’ll give credit to you all if and when I release the gamemode. I appreciate it.