Hooking when player uses a key for running a client side command.

Hello Facepuch users.

This is actually my first post but I’ve been on the forums for lot of time and really helped me with many things.

As I’m starting this thread I’m needing help with a small LUA code that I’m trying to do. I am not an experienced user with LUA, I’m mostly with PHP, Perl and those.
I would please ask for help on this small thing which I think will be easy for the experienced users… Actually I’m trying to run a command when the player is on a vehicle and he press SHIFT Key (KEY_LSHIFT)

Here I attach the small code which I tried to do.



function VehicleSirenCheck()
	ply = LocalPlayer()
	if ply:InVehicle() then
		return true
	else
		return false
	end
end

local Think = function()
	if VehicleCheck()
		if LocalPlayer():KeyDown(KEY_LSHIFT) then
		ply:ConCommand("VC_ToggleSirenOnOff")
		end
	end
end
 
hook.Add("Think", "My button hook", VehicleSirenCheck)



Is it correct? Do I have to do something else?
I’m really not sure on this language, it’s really weird to me, so excuse me if I’m too ignorant on this.

Thanks for reading and helping.
I appreciate it.

With best regards,
Ivan.

In VehicleSirenCheck why are you making a ply variable if you only use ply once, replace ply with LocalPlayer().

Also it is more common to use local function Think() than local think = function()

In the Think function the ply variable isn’t set.

The Think function won’t run, cause there is no code that runs it.

Thanks for the answer.

I’ve done the changes you said, but I think I still need help, hehe. So, if this code would work, where would I need to insert this code it at? cl_init, init or shared? Or, would it work as a normal addon?

This lua language still gets me some headache, it’s really weird to me the way it’s coded lol.



function VehicleCheck()
	if LocalPlayer():InVehicle() then
		return true
	else
		return false
	end
end

local function SirenOnOff()
	if VehicleCheck()
		if LocalPlayer():KeyDown(KEY_LSHIFT) then
		LocalPlayer():ConCommand("VC_ToggleSirenOnOff")
		end
	end
end
 
hook.Add("VehicleCheck", "My button hook", SirenOnOff)****


EDIT: I’ve done it by other way and it works.

Thanks for the reply!
Some Mod please close the thread.