Problems with KeyPress and Think

I’m wanting to change a players velocity when the space key is held/pushed down. I’m at a complete loss so I com here hoping you guys can help me out

I’ve come up with this:


hook.Add( "Think", "upveloc", function( ply, key )
	if ( key == IN_JUMP ) then
		ply:SetVelocity(ply:GetUp() * 9)
	end
end )

perhaps this?:



function GM:KeyPress( ply, key )
	if ( key == IN_JUMP ) then
		ply:SetVelocity(ply:GetUp() * 9)
	end
end

I’m trying to get the same affect as the jump pack from the original pointshop.

tried player:SetJumpPower?

The Up velocity with that code cancels out things like the vents on the map dolls and teen room and provides an up “push” while falling. I’m not looking to actually jump higher

Since you want it ‘jetpack-like’, you need KeyDown, so it runs the code aslong as the key is held down:

Player:KeyDown

So would it be:



hook.Add( "Tick", "upveloc", function()
	if ( ply:KeyDown( IN_JUMP ) ) then
		ply:SetVelocity(ply:GetUp() * 9)
	end
end )

or



hook.Add( "Tick", "upveloc", function( ply )
	if ( ply:KeyDown( IN_JUMP ) ) then
		ply:SetVelocity(ply:GetUp() * 9)
	end
end )

Sorry functions really screw me up, and I can just throw this in lua/autorun/server correct?

use the first, and either replace ‘ply’ with LocalPlayer() or set the variable ply as LocalPlayer().

Sorry, feels like your having to “hold my hand” through this but:



hook.Add( "Tick", "upveloc", function()
	if ( LocalPlayer():KeyDown( IN_JUMP ) ) then
		LocalPlayer():SetVelocity(LocalPlayer():GetUp() * 9)
	end
end )

I did that and put it in lua/autorun/server

try client, I never tried SetVelocity on client myself, but it should work in autorun/client.

[ERROR] lua/autorun/client/upvel.lua:4: attempt to call method ‘KeyDown’ (a nil value)

  1. fn - lua/autorun/client/upvel.lua:4
  2. unknown - addons/ulib-master/lua/ulib/shared/hook.lua:110

[editline]6th February 2016[/editline]

I also tried it in autorun/server that didn’t work either but it didn’t give an error

You’ll have to (serverside) loop through all players with

Global.ipairs on

player.GetAll, checking if they’re pressing the button.

Player:KeyDown is shared but

Entity:SetVelocity is server only (since everyone would be able to make hacks if it worked clientside too, in the client it works only for prediction purposes)

Thanks I got what I was looking for working

this can be closed or what not