WasKeyPressed not working

So, I’d like to lock and unlock vehicle’s from inside the car.
When a player presses L it should say lock. Next time on press Unlock.
But the function WasKeyPressed isn’t working the way it should. I’m getting everytime attempted to index bla bla bla (A nil value)
How can I make the WasKeyPressed working?



AddCSLuaFile("autorun/client/dmod_locker.lua")

resource.AddSingleFile("sound/vehicles/lock.mp3")
util.PrecacheSound("vehicles/lock.mp3")

resource.AddSingleFile("sound/vehicles/unlock.mp3")
util.PrecacheSound("vehicles/unlock.mp3")

hook.Add( "Think", "CheckKeyThing", function( ply, key )

	local ply = LocalPlayer()

    if input.IsKeyDown( KEY_L ) and ply:InVehicle() then
		lock = true	
	elseif input.IsKeyDown( KEY_L ) and ply:InVehicle() then
		lock = false
    end
	
	if lock == true and ply:InVehicle() then
		notification.AddLegacy( "You have locked the car!", NOTIFY_GENERIC, 3 )
	elseif lock == false and ply:InVehicle() then
		notification.AddLegacy( "You have unlocked the car!", NOTIFY_GENERIC, 3 )
	end
end )


I’d guess no one is actually known with input.WasKeyPressed :S

Why don’t you use KeyPress hook instead of Think hook which is not made for what you want to create.

The KeyPress hook doesn’t work with the L key

How will I make it work then?

Well, I’m not sure exactly how to fix it, but here’s a few points:



if input.IsKeyDown( KEY_L ) and ply:InVehicle() then
		lock = true	
	elseif input.IsKeyDown( KEY_L ) and ply:InVehicle() then
		lock = false
    end


This thing here doesn’t make any sense - you’re checking the same thing two times and expecting it to give different results… also, you seem to be using the lock variable globally, which doesn’t make much sense to me since that’d make it get changed whenever ANY player locks or unlocks ANY vehicle.

Honestly, your original code is very odd, and I’m not quite sure why you scripted it the way you did … I don’t mean to spoonfeed, but try this instead:



local pressedlast = false -- this variable is very important because it stops the car being locked and unlocked all the time

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

	local ply = LocalPlayer()
	local inveh = ply:InVehicle()
	local veh = ply:GetVehicle()
	
	if !inveh or !IsValid( veh ) then return end -- don't run the rest of the script if the vehicle isn't valid or the player isn't in a vehicle
	
	veh.lock = veh.lock or false -- set a variable LOCAL TO THE VEHICLE rather than globally
	
	if input.IsKeyDown( KEY_L ) then
		if !pressedlast then -- if KEY_L was NOT pressed last time this was called, then
			veh.lock = !veh.lock -- this will invert the variable, meaning if the vehicle was locked, it'd get unlocked and the reverse if it was unlocked
			if veh.lock then
				notification.AddLegacy( "You have locked the car!", NOTIFY_GENERIC, 3 )
			else
				notification.AddLegacy( "You have unlocked the car!", NOTIFY_GENERIC, 3 )
			end
			pressedlast = true -- set it to be pressed so this won't be run unless the key was released between calls
		end
	else
		pressedlast = false -- set the variable as false if the key ISN'T down
	end
	
end )