Question Regarding Player Classes

How would I go about checking to see if a player is a certain class before or during a function? I have tried setting up player classes under teams, but something always seems to bug out for me. I enjoy player classes a lot, but there doesn’t seem to be much documentation.

So in case I wasn’t really clear in what I was asking, how would I run something like…

if ply:Team() == 1 then

But instead for player classes?

Look at this code:

local function LookupPlayerClass( ply )

	local id = ply:GetClassID()
	if ( id == 0 ) then return end

	-- Check the cache
	local method = ply.m_CurrentPlayerClass;
	if ( method && method.Player == ply && method.ClassID == id && method.Func ) then return method end

	-- No class, lets create one
	local classname = util.NetworkIDToString( id )
	if ( !classname ) then return end

	-- Get that type. Fail if we don't have the type.
	local t = Type[ classname ]
	if ( !t ) then return end

	local method = {}
		method.Player	= ply
		method.ClassID	= id
		method.Func		= function() end
	setmetatable( method, { __index = t } )

	ply.m_CurrentPlayerClass	= method

	return method


I’ve looked at this code over and over again and have fiddled with a bunch of different functions within the player_manager.lua, but I still can’t find out what to do. I’m sorry if I’m not understanding something that should be obvious, I’m very new at player classes and fairly new to lua as a whole.


function GetPlayerClass( ply )
    local id = ply:GetClassID()
    if ( id == 0 ) then return end

    return util.NetworkIDToString( id )

Not sure why, but this worked for me.

Alright so…

if ply:GetClassID() == 27 then

worked for me, but I had to do…

print( ply:GetClassID() )

to find out that the code for player_citizen was 27.

Is there a way to choose what number you want associated with the Player Class? Or better yet, be able to do…

if ply:GetClassID() == "player_citizen" then

or something similar instead?

I posted a function for you damit, use it.

Lol. I tried, it just kept saying GetPlayerClass was a nil value no matter where I put it. Idk why it worked fine for you, but not for me…

You use it like this: GetPlayerClass( ply ) not like this: ply:GetPlayerClass().

Alright. Sorry for the misunderstanding, it works perfectly. Thanks for all the help and bearing with me Robotboy.