Help Thirdperson

Hello,

I am trying to make a little thirdperson addon but it’s not easy when you are a fledgling member …

I try to add a command to switch to firstperson to thirdperson and also make a command to execute the function

But I don’t know what is the function to go back firstperson … here is the code :

I put this file in lua/autorun but I am not sure if it is a good idea to place it there, because when you join the server you are automaticaly in thridperson view :confused:

For the command I suppose I have to use this one with the function Firstperson (to go back first person) and do the same for thirdperson but I am a bit lost …

Some help is welcome

In your CalcView function, do a check right at the top:

[lua]if ( !LocalPlayer( )._inThirdPerson ) then return; end[/lua]

I wouldn’t recommend using concommands for toggling third person.

I personally use F3 for thirdperson, these are the f1-f4 hook names

F1 ShowHelp
F2 ShowTeam
F3 ShowSpare1
F4 ShowSpare2

So, for F3:

[lua]hook.Add( “ShowSpare1”, “ToggleThirdperson”, function( )
LocalPlayer( )._inThirdPerson = !LocalPlayer( )._inThirdPerson
end )[/lua]

Ok thanks for you help but as I am a beginner in lua I still have a problem here is the new code based on what you said but I don’t know if I made things correctly :


function ThirdPerson(ply, pos, angles, fov)

    local view = {}
    view.origin = pos-(angles:Forward()*150) +
                            (angles:Right()*1) +
                                    (angles:Up()*8)
        view.angles = angles
        view.fov = fov
 
    return view
end
 
 hook.Add( "ShowSpare1", "ToggleThirdperson", function( ) -------F3
 
	LocalPlayer( )._inThirdPerson = !LocalPlayer( )._inThirdPerson
	
	
end )


 hook.Add("CalcView", "ThirdPerson", ThirdPerson)
 if ( !LocalPlayer( )._inThirdPerson ) then return; end ------ CHECK
 
 hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) 
         return true 
 end)
  
 function HUDPaint()
     local ply = LocalPlayer();
     local p = LocalPlayer():GetEyeTrace().HitPos:ToScreen()
     local x,y = p.x, p.y
     
     draw.SimpleText( '+', 'Default', x, y, color_white, 1, 1 )
 
 end

Lua error :

[ERROR] lua/autorun/thirdperson.lua:19: attempt to call global ‘LocalPlayer’ (a nil value)

  1. v - lua/autorun/thirdperson.lua:19
  2. unknown - lua/includes/modules/hook.lua:82

Is it trying to call a not existing function ?

Here’s a simplified version of your original code, which should do the same thing - I removed the ShouldDrawLocalPlayer, and copied in the rest.

[lua]// Stand-alone hook instead of function linked hook
hook.Add(“CalcView”, “ThirdPerson”, function( ply, pos, angles, fov )
// Stop this hook from doing it’s thing if we’re not in third person
if ( !ply._inThirdPerson ) then return; end

local view = {
	origin = ply:GetShootPos( ) + 
			( ply:EyeAngles( ):Right( ) * 50 ) + 
			( Angle( 0, ply:EyeAngles( ).y, 0 ):Forward( ) * -150 ) + 
			( Angle( 0, 0, ply:EyeAngles( ).z ):Up( ) * 20 );
	angles = angles;
	fov = fov;
	drawviewer = ply._inThirdPerson; // Same as ShouldDrawLocalPlayer - if it's nil or false it doesn't draw, true makes player draw
}

return view

end )

// F3 hook
hook.Add( “ShowSpare1”, “ToggleThirdperson”, function( )
LocalPlayer( )._inThirdPerson = !LocalPlayer( )._inThirdPerson
end )

//
hook.Add( “HUDPaint”, “3DCrossHair”, function( )
local ply = LocalPlayer( );

// Stop this hook from doing it's thing if we're not in third person
if ( !ply._inThirdPerson ) then return; end

local p = ply:GetEyeTrace( ).HitPos:ToScreen( )
local x, y = p.x, p.y

draw.SimpleText( '+', 'Default', x, y, color_white, 1, 1 )

end )[/lua]

The code you responded with is… broken… It’s missing things here and there. Hopefully this snippet helps you learn :slight_smile:

Here’s the issues I saw with your new code aside from tabbing and spacing being off:


// Seems fine
function ThirdPerson(ply, pos, angles, fov)

    local view = {}
    view.origin = pos-(angles:Forward()*150) +
                            (angles:Right()*1) +
                                    (angles:Up()*8)
        view.angles = angles
        view.fov = fov
 
    return view
end
 
// Seems fine
 hook.Add( "ShowSpare1", "ToggleThirdperson", function( ) -------F3
 
	LocalPlayer( )._inThirdPerson = !LocalPlayer( )._inThirdPerson
	
	
end )


// Seems fine
 hook.Add("CalcView", "ThirdPerson", ThirdPerson)

// This needs to be in the thirdperson function, and also in should draw local player, and in hud paint - so because this is standing by itself and autorun runs on the server, you're getting the error
 if ( !LocalPlayer( )._inThirdPerson ) then return; end ------ CHECK
 
// Not needed with other code
 hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) 
         return true 
 end)
  
// seems fine
 function HUDPaint()
     local ply = LocalPlayer();
     local p = LocalPlayer():GetEyeTrace().HitPos:ToScreen()
     local x,y = p.x, p.y
     
     draw.SimpleText( '+', 'Default', x, y, color_white, 1, 1 )
 
 end

Oh ok I understood my errors but It is still showing the error of attempting to call a no existing function I don’t understand why :

[ERROR] lua/autorun/thirdperson.lua:23: attempt to call global ‘LocalPlayer’ (a nil value)

  1. v - lua/autorun/thirdperson.lua:23
  2. unknown - lua/includes/modules/hook.lua:82

I used your code :


// Stand-alone hook instead of function linked hook
hook.Add("CalcView", "ThirdPerson", function( ply, pos, angles, fov )
	// Stop this hook from doing it's thing if we're not in third person
	if ( !ply._inThirdPerson ) then return; end
	
	local view = {
		origin = ply:GetShootPos( ) + 
				( ply:EyeAngles( ):Right( ) * 50 ) + 
				( Angle( 0, ply:EyeAngles( ).y, 0 ):Forward( ) * -150 ) + 
				( Angle( 0, 0, ply:EyeAngles( ).z ):Up( ) * 20 );
		angles = angles;
		fov = fov;
		drawviewer = ply._inThirdPerson; // Same as ShouldDrawLocalPlayer - if it's nil or false it doesn't draw, true makes player draw
	}

	return view
end )

// F3 hook
hook.Add( "ShowSpare1", "ToggleThirdperson", function( )
	LocalPlayer( )._inThirdPerson = !LocalPlayer( )._inThirdPerson
end )

// 
hook.Add( "HUDPaint", "3DCrossHair", function( )
	local ply = LocalPlayer( );
	
	// Stop this hook from doing it's thing if we're not in third person
	if ( !ply._inThirdPerson ) then return; end
	
	local p = ply:GetEyeTrace( ).HitPos:ToScreen( )
	local x, y = p.x, p.y

	draw.SimpleText( '+', 'Default', x, y, color_white, 1, 1 )
end )

And also I tried the code with my fault solved by your explanation and still the same error :tinfoil:


function ThirdPerson(ply, pos, angles, fov)
if ( !LocalPlayer( )._inThirdPerson ) then return; end ---I added the code here
    local view = {}
    view.origin = pos-(angles:Forward()*150) +
                            (angles:Right()*1) +
                                    (angles:Up()*8)
        view.angles = angles
        view.fov = fov
 
    return view
end
 
 hook.Add( "ShowSpare1", "ToggleThirdperson", function( ) -------F3
 if ( !LocalPlayer( )._inThirdPerson ) then return; end   ---- here
	LocalPlayer( )._inThirdPerson = !LocalPlayer( )._inThirdPerson
	
	
end )


 hook.Add("CalcView", "ThirdPerson", ThirdPerson)
 if ( !LocalPlayer( )._inThirdPerson ) then return; end ------ CHECK
 
  hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) 
         return true 
		 end)
  
  
 function HUDPaint()
 if ( !LocalPlayer( )._inThirdPerson ) then return; end --- and here
     local ply = LocalPlayer();
     local p = LocalPlayer():GetEyeTrace().HitPos:ToScreen()
     local x,y = p.x, p.y
     
     draw.SimpleText( '+', 'Default', x, y, color_white, 1, 1 )
 
 end

Thank you to take time to help me ^^

Because LocalPlayer() doesn’t work in singleplayer :wink:

Ok thanks

(putin de connerie x) )

The problem is because some of your code needs to be run serverside and some of it clientside.
When a player presses F3, it calls the hook on the server. The server then needs to tell the client to turn on third person. I have cleaned it up to use a concommand to get the server to make the client run a function. If you don’t want people to manually run the command you could instead use the net library to pass data from server to client,

[lua]if SERVER then
AddCSLuaFile() --send this file to the client

hook.Add( "ShowSpare1", "ToggleThirdperson", function(ply) -------ply pressed f3
	ply:ConCommand("toggle_my_thirdperson") --make the player run the command on their client
end )

end

if CLIENT then

local function ThirdPerson(ply, pos, angles, fov)
	if ( LocalPlayer()._inThirdPerson ) then --only run if in third person
		local view = {}
		view.origin = pos-(angles:Forward()*150) +
								(angles:Right()*1) +
										(angles:Up()*8)
			view.angles = angles
			view.fov = fov
	 
		return view
	end
end
hook.Add("CalcView", "My_ThirdPerson", ThirdPerson)
hook.Add("ShouldDrawLocalPlayer", "My_ThirdPerson_ShouldDrawLocalPlayer", function()
	return (LocalPlayer()._inThirdPerson == true)
end)

local function HUDPaint()
	if ( !LocalPlayer()._inThirdPerson ) then return end
	local ply = LocalPlayer();
	local p = LocalPlayer():GetEyeTrace().HitPos:ToScreen()
	local x,y = p.x, p.y

	draw.SimpleText( '+', 'Default', x, y, color_white, 1, 1 )
end
hook.Add("HUDPaint", "My_ThirdPerson_HUDPaint", HUDPaint)

--Command for toggling thirdperson (command exists clientside)
concommand.Add("toggle_my_thirdperson", function()
	if LocalPlayer()._inThirdPerson == nil then
		LocalPlayer()._inThirdPerson = false --default to false the first time
	end
	LocalPlayer()._inThirdPerson = not LocalPlayer()._inThirdPerson --flip it to true or false
end)

end[/lua]

[editline]23rd July 2013[/editline]

Wtf? I am seeing =“keyword”> randomly in the code…

Thank you so much for your detailed explanation !

Good , when someone help him on his code , he sell the script on CoderHire.

Never sold a script concerning Thirdperson on coderhire, it was just for my personals knowledges.

Don’t you think that if I really wanted to sell a thirdperson code stolen, I will not think to edit/delete this post ?

This topic will give the solution to players who have the same problem. And in addition why selling a thirdperson script on CoderHire if it is available here for free ?

Strange, you’re banned from CoderHire.

Let’s call Inspector Gadget !

I actually do all of my third person code client-side. You don’t need to do anything server-side. to achieve third person… Why have the server do logic it needs to know nothing about?

I like to have the server and/or other clients know if people are in third person so other addons can take it into account.

In particular there is/was a bug where if you are in third person (using chasecam or another script that functions similar) and you exit an SCar, your player entity gets removed. This causes ULX to reset your rank. I had to write a fix for it so I needed to know whether people were in third person while exitting.

Incredible. Can’t say I’ve ever experienced anything like that O_o. Then again, I code everything I use. For my third-person / over-the-shoulder, I take into consideration whether or not they’re in a vehicle. If they are, I allow the vehicle third-person code to run. When they get out of the car, my code runs. So that way there is no double third-person logic being executed.

Is there a way to make it so by pressing F6 it makes the player go in third person?
Thanks
EDIT: I was able to allow it to use F2 but when I press it in-game nothing happens, its only accessible when typed into console.
Anyone know how to fix please?
Thanks!