Toggle between third person with F3?

Heads up: this is my first post so please don’t get annoyed if I’ve put this in the wrong thread.

Hey everyone,
Alright so I’m having trouble trying to toggle third person with F3, I’ve already got some code to toggle third person with a console command, but I cannot seem to get it working with the F3 toggling in it.
Here’s my current code:



if ( CLIENT ) then
	local on = true 

	local function toggle()
		on = !on
		if on == true then else end
	end

	hook.Add( "ShouldDrawLocalPlayer", "ThirdPersonDrawPlayer", function()
		if on and LocalPlayer():Alive() then
			return true
		end
	end )
	hook.Add( "CalcView", "ThirdPersonView", function( ply, pos, angles, fov )
		if on and ply:Alive() then
			local view = {}
			view.origin = pos - ( angles:Forward() * 70 ) + ( angles:Right() * 20 ) + ( angles:Up() * 5 )
			view.angles = ply:EyeAngles() + Angle( 1, 1, 0 )
			view.fov = fov

			return GAMEMODE:CalcView( ply, view.origin, view.angles, view.fov )
		end
	end )
	concommand.Add( "thirdperson_toggle", toggle )
end


Help is much appreciated :slight_smile:

For F3 it would be



hook.Add("ShowSpare1", "unique name", function(ply)
	ply:ConCommand("console command here")
end)


Some other F keys are:



	F1 = "ShowHelp"
	F2 = "ShowTeam"
	F3 = "ShowSpare1"
	F4 = "ShowSpare2"


All alright, thanks for that :slight_smile:

-snip-

Hmm, it doesn’t seem to be working man. Thanks though.

Here is what I have now:



if ( CLIENT ) then
	local on = false 

	local function toggle()
		on = !on
		if on == true then else end
	end

	hook.Add( "ShouldDrawLocalPlayer", "ThirdPersonDrawPlayer", function()
		if on and LocalPlayer():Alive() then
			return true
		end
	end )
	hook.Add( "CalcView", "ThirdPersonView", function( ply, pos, angles, fov )
		if on and ply:Alive() then
			local view = {}
			view.origin = pos - ( angles:Forward() * 70 ) + ( angles:Right() * 20 ) + ( angles:Up() * 5 )
			view.angles = ply:EyeAngles() + Angle( 1, 1, 0 )
			view.fov = fov

			return GAMEMODE:CalcView( ply, view.origin, view.angles, view.fov )
		end
	end )
	concommand.Add( "thirdperson_toggle", toggle )

	hook.Add("ShowSpare1", "ThirdPersonToggle", function( ply )
		ply:RunConCommand("thirdperson_toggle") -- A friend of mine told me to use RunConCommand instead of ConCommand since it's client side.
	end)
end


Edit: I have tried adding:



local ply = LocalPlayer()


at the top, but that doesn’t help with anything :confused:

The way I did it was using the util.AddNetworkString but I do not think it is the best way. But since you are doing it in one code I guess I would do it this way.
Maybe this will work:



if ( CLIENT ) then
	local on = false 

	local function toggle()
		on = !on
		if on == true then else end
	end

	hook.Add( "ShouldDrawLocalPlayer", "ThirdPersonDrawPlayer", function()
		if on and LocalPlayer():Alive() then
			return true
		end
	end )
	hook.Add( "CalcView", "ThirdPersonView", function( ply, pos, angles, fov )
		if on and ply:Alive() then
			local view = {}
			view.origin = pos - ( angles:Forward() * 70 ) + ( angles:Right() * 20 ) + ( angles:Up() * 5 )
			view.angles = ply:EyeAngles() + Angle( 1, 1, 0 )
			view.fov = fov

			return GAMEMODE:CalcView( ply, view.origin, view.angles, view.fov )
		end
	end )
	concommand.Add( "thirdperson_toggle", toggle )

	hook.Add("ShowSpare1", "ThirdPersonToggle", function( ply )
		ply:RunConCommand("thirdperson_toggle") -- A friend of mine told me to use RunConCommand instead of ConCommand since it's client side.
	end)
elseif ( SERVER ) then
   function GM:ShowSpare1( ply )
   	   toggle()
   end
end


If that does not work I would try removing local from the function on line 4.

Nevermind guys, I managed to fix it. For anyone who has the same issue here is the code:



function GM:ShowSpare1( ply )
	ply:ConCommand("thirdperson_toggle")
end