My code keeps crashing :/

Hey guys I’ve tried all the ways i can think of to create a function which will run when you press a key, record the time the key is held down for and store it as a variable. My code either doesn’t run (even with no lua errors) or it gets stuck in an infinite loop which crashes gmod. It would really help me if someone could post the code I need because I must be missing something :s Thanks

Your code?

Urgh, does nobody read the sticky.

POST THE FULL ERROR AND THE CODE

Sorry guys I intentionally excluded the code because I thought it would be easier to find the function I wanted out of the context that I want it for. Here’s the code I’ve ended up with at the moment: (this one apparently causes an infinite loop though i can’t figure out why or what i should replace it with)

function GM:Move( ply, move )

if ( move:KeyPressed( IN_JUMP ) ) then
local time = 0
local jump = Vector(0, 0, 1)

repeat
time = time + 1
until not( move:KeyDown( IN_JUMP ) )

jump = jump*time
move:SetVelocity( move:GetVelocity() + jump )

end

return true
end

[lua]repeat
time = time + 1
until not move:KeyDown(IN_JUMP)[/lua]

The movedata object isn’t going to change during this loop. GM:Move() gets called repeatedly for separate objects.

uhm okay, sorry I’m really new to this but do you know what condition i should use to check whether the key is still held down then?

I would suggest keeping a table of players you are currently keeping track of, and their associated timers and jump key states. Design a data structure that holds the timing, key state, and player ID of every currently tracked player. With each call of your function, update the timers and key states for each player in the table. If one of the players’ key states changes to an unpressed state, perform the jump and remove the player from the table.

yeah that sounds like a good idea, I guess I’m missing some basic understanding of hooks and stuff because I can’t get the code to work for a single player at the moment. Would i have to make my own function to add the jump to a specific player and use hook.add with gm:move to supply it with the necessary movedata?

[lua]
function GM:Move( ply, move )
local time = 0
if ( move:KeyPressed( IN_JUMP ) ) then
time = time + 1
local jump = Vector(0, 0, 1)
elseif !( move:KeyDown( IN_JUMP ) )

jump = jump*time
move:SetVelocity( move:GetVelocity() + jump )
time = 0

end

return true
end
[/lua]
something like this maybe? Edit time, since itt’l probably revert to 0 on each move run.

It would make much more sense to store the data on the individual players, something like this [lua]
function GM:Move( ply, move )
if move:KeyPressed( IN_JUMP ) then
ply.PowerJumpStart = CurTime()
ply.PowerJumpWasPressed = true

	--Put your stuff for when the key is pressed here
elseif move:KeyDown( IN_JUMP ) then
	--Your stuff for when they key's held here (If you need it)
elseif ply.PowerJumpWasPressed then --We've got a boolean here so it's easy to tell it was pressed, can probably use move:KeyReleased( IN_JUMP ) or move:KeyWasDown( IN_JUMP ) instead
	local HoldTime = CurTime()-ply.PowerJumpStart
	
	ply.PowerJumpStart = nil
	ply.PowerJumpWasPressed = false
	
	--Put your stuff for when the key is released here
end

return true 

end[/lua]

Then you don’t have a table full of disconnected or invalid players to clean out

Cool thanks for the help guys :slight_smile: