Thirdperson Lua

Alright, so i found this code and I edited it a bit. It is supposed to give the player the command “/thirdperson” and “/firstperson” to go in and out of thirdperson (without sv_cheats). The code works, but I wanna make it only work for my donator, moderator, admin, AND superadmin groups. Right now, it only works for superadmin. Here’s the code:


if ( SERVER ) then

	function ThirdPersonPlayerSay( Player, Text, Public )
		if Player:IsUserGroup{"superadmin", "admin", "moderator", "donator"} then
			if ( Player:IsValid() ) then
				if ( string.lower( Text ) == "/thirdperson" ) then
					Player:ConCommand( "ls_thirdperson 1" )
					return ""
				elseif ( string.lower( Text ) == "/firstperson" ) then
					Player:ConCommand( "ls_thirdperson 0" )
					return ""
				end
			end
		else
			ULib.tsayColor(Player, Color(255, 0, 0, 0), "You don't have that ability!")
		end
	end
	hook.Add( "PlayerSay", "ThirdPersonPlayerSay", ThirdPersonPlayerSay )

end

LsThirdPerson = CreateConVar( "ls_thirdperson", "0", {

	FCVAR_ARCHIVE

} )

if ( CLIENT ) then

	-- You should only modify the following table values, unless you know what you're doing.

	local CameraOffset = { -- Allows you to offset the camera however you like.

		UD = 0, -- Up / Down. Use a positive number for up, negative for right. Default = 0
		RL = 20, -- Right / Left. Use a positive number for right, negative for left. Default = 0
		FB = -70 -- Forward / Backward. Use a positive number for forward ( Not sure why you would do it ), negative for backward. Default = -88

	}

	function ThirdPersonDrawPlayer()

		return LsThirdPerson:GetBool() and not ( LocalPlayer():InVehicle() ) or LocalPlayer():IsPlayingTaunt()

	end
	hook.Add( "ShouldDrawLocalPlayer", "ThirdPersonDrawPlayer", ThirdPersonDrawPlayer )

	function ThirdPersonCalcView( Player, Origin, Ang, Fov )

		if ( LsThirdPerson:GetBool() and not ( Player:InVehicle() ) ) then

			local TraceData = { }

			TraceData.start = Player:EyePos()
			TraceData.endpos = Player:EyePos() + ( Player:GetUp() * CameraOffset.UD ) + ( Player:GetRight() * CameraOffset.RL ) + ( Player:GetForward() * CameraOffset.FB )
			TraceData.filter = Player
			TraceData.mask = MASK_SOLID_BRUSHONLY

			local Trace = util.TraceLine( TraceData )

			local View = { }
			View.origin = Trace.HitPos + Player:GetForward() * 8
			View.angles = Player:GetAngles()
			View.fov = Fov

			return View

		end

	end
	hook.Add( "CalcView", "ThirdPersonCalcView", ThirdPersonCalcView )

end

I tried using () instead of {} as well on IsUserGroup, but it still didn’t work.

You can’t add multiple entries like that. If you want to use a table of different groups, use table.HasValue. Add this above your ThirdPersonPlayerSay function.


local thirdGroup = { "superadmin", "admin", "moderator", "donator" }

Change your group check if statement to


if ( table.HasValue( thirdGroup, Player:GetNWString( "usergroup" ) ) ) then

Alright thanks!! However, my server happens to be a prop hunt server, and if a hunter uses this during the starting blind time, they can see.

I was taught to avoid GLua functions like table.HasValue() so I would do



local thirdGroup = { ["superadmin"] = true, ["admin"] = true, ["moderator"] = true, ["donator"] = true }
if ( thirdGroup[ Player:GetNWString( "usergroup" ] ) ) then


Don’t know which is more efficient

You don’t need [" and "] for standard strings, just use local thirdGroup = { superadmin = true, admin = true … } etc…

then thirdGroup[ NWString ]…

While NW string can be slow, I recommend caching: https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_nw_vars.lua.html

And using HasValue is expensive: https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_hud_stuff.lua.html

Normally I’d recommend avoiding NW vars all together but GMod uses it for usergroups.

I forgot to provide a “live” caching example: https://dl.dropboxusercontent.com/u/26074909/tutoring/making_players_invisible.lua.html

Definitely check out how slow NW can be, and an update every second is sufficient to help speed things up.

https://dl.dropboxusercontent.com/u/26074909/tutoring/tracking_players/entity_setowner_and_getowner_rewrite.lua.html

Okay I sort of get what benchmarking is from the examples, however I have never worked with it. The code works with code_gs fix, however I want it to run its best so my server won’t lag or crash. So can you help me understand or link me to where I can learn more about it? (Learning LUA). Also that last one you posted, would that work for props?

Although table.HasValue is expensive, it won’t be an issue unless someone spams /thirdperson in chat. Acecool and Anon are right, though.

The code works fine but I notice fps loss. I think it has to do with the tracedata.