Resetting Text Above Players Head (Reverts back after 5 seconds)

I am trying to create a text above the players head that shows their “Role” they currently are.

It works great but when changing roles it displays that you have changed and shows the correct role name but only for a certain amount of time before it reverts back to “Rebel”

Snippet of code for the drawing:


function DrawName( ply )
 
	if !ply:Alive() then return end
 
	local offset = Vector( 0, 0, 85 )
	local offset2 = Vector( 0, 0, 80 )
	local ang = LocalPlayer():EyeAngles()
	local pos = ply:GetPos() + offset + ang:Up()
 local pos2 = ply:GetPos() + offset2 + ang:Up()
	ang:RotateAroundAxis( ang:Forward(), 90 )
	ang:RotateAroundAxis( ang:Right(), 90 )
	
	if (ply:IsMetroCop() == 1) then
	local role = "MetroCop"
		elseif(ply:IsCombineTactical() == 1)then
		role = "CombineTactical"
			elseif(ply:IsCombineElite() == 1) then
			role = "Combine Elite"
				elseif(ply:IsCombineBreacher() == 1)then
				role = "Combine Breacher"
					elseif(ply:IsCombineSergeant() == 1) then
					role = "Combine Sergeant"
						elseif(ply:IsCombineEnforcer() == 1)then
						role = "Combine Enforcer"
							elseif(ply:IsCombineCommander() == 1) then
							role = "Combine Commander"
								elseif(ply:IsCombineSniper() == 1) then
								role = "Combine Sniper"
									elseif(ply:IsCombineJailGuard() == 1)then
									role = "Combine Jailguard"
										elseif(ply:IsRebel() == 1) then
										role = "Rebel"
											else
											role = "N/A"
	
	end
 
	cam.Start3D2D( pos, Angle( 0, ang.y, 90 ), 0.25 )
		draw.DrawText( ply:GetName(), "BudgetLabel", 2, 2, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER )
	cam.End3D2D()
 cam.Start3D2D( pos2, Angle( 0, ang.y, 90 ), 0.25 )
		draw.DrawText( role, "BudgetLabel", 2, 2, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER )
	cam.End3D2D()
end

hook.Add( "PostPlayerDraw", "DrawName", DrawName )

hook.Add("PlayerSpawn", "Redraw", DrawName)

Pictures of what it looks like:
The way it’s supposed to be:

What it reverts back to:

sh_meta.lua:


function metatab:IsMetroCop()

	if( self:GetNWInt( "Role" ) == 1) then
		return 1
	else
		return 0
	end

end

function metatab:IsCombineTactical()

	if( self:GetNWInt( "Role" ) == 2) then
		return 1
	else
		return 0
	end

end

function metatab:IsCombineElite()

	if( self:GetNWInt( "Role" ) == 3) then
		return 1
	else
		return 0
	end

end

function metatab:IsCombineBreacher()

	if( self:GetNWInt( "Role" ) == 4) then
		return 1
	else
		return 0
	end

end

function metatab:IsCombineSergeant()

	if( self:GetNWInt( "Role" ) == 5) then
		return 1
	else
		return 0
	end

end

function metatab:IsCombineEnforcer()

	if( self:GetNWInt( "Role" ) == 6) then
		return 1
	else
		return 0
	end

end

function metatab:IsCombineCommander()

	if( self:GetNWInt( "Role" ) == 7) then
		return 1
	else
		return 0
	end

end

function metatab:IsCombineSniper()

	if( self:GetNWInt( "Role" ) == 8) then
		return 1
	else
		return 0
	end

end

function metatab:IsCombineJailGuard()

	if( self:GetNWInt( "Role" ) == 9) then
		return 1
	else
		return 0
	end

end

function metatab:IsRebel()

	if( self:GetNWInt( "Role" ) == 10) then
		return 1
	else
		return 0
	end

end
----------------------------------------------





--[[SAVE THE LAST ROLE THE PLAYER WAS]]--

function metatab:SaveRole()
	self:SetPData( "Role", self:GetNWInt( "Role" ) )
end
----------------------------------------------------




--[[ADD MAKE TEAMS AFTER HERE]]--

function metatab:makeMetroCop()
	self:SetNWInt( "Role", 1 )
	self:SaveRole()
end

function metatab:makeCombineTactical()
	self:SetNWInt( "Role", 2 )
		
	self:SaveRole()
end

function metatab:makeCombineElite()
	self:SetNWInt( "Role", 3 )
	
	self:SaveRole()
end

function metatab:makeCombineBreacher()
	self:SetNWInt( "Role", 4 )
		
	self:SaveRole()
end

function metatab:makeCombineSergeant()
	self:SetNWInt( "Role", 5 )
		
self:SaveRole()
end


function metatab:makeCombineShotgunner()
	self:SetNWInt( "Role", 6 )
	/*	self:SetModel("models/player/Combine_Soldier.mdl")
	self:Give("weapon_stunstick","weapon_shotgun")
	self:Spawn()*/
self:SaveRole()
end

function metatab:makeCombineCommander()
	self:SetNWInt( "Role", 7 )
	/*self:SetModel("models/player/breen.mdl")
	self:Give("weapon_stunstick")
	self:Spawn()*/
	SaveRole()
end

function metatab:makeCombineSniper()
	self:SetNWInt( "Role", 8 )
		/*self:SetModel("models/player/barney.mdl")
	self:Give("weapon_stunstick","weapon_crossbow")
	self:Spawn()*/
	SaveRole()
end

function metatab:makeCombineJailGuard()
	self:SetNWInt( "Role", 9 )
	/*self:SetModel("models/player/Combine_Soldier_PrisonGuard.mdl")
	self:Give("weapon_stunstick","weapon_shotgun")
self:Spawn()*/
	SaveRole()
end

function metatab:makeRebel()
	self:SetNWInt( "Role", 10 )
self:SaveRole()
end



Thanks for all of the help you guys give C:

Holy fuck that’s a lot of elseifs.
First off, let’s make this simpler, why are you making so many different team functions? Why don’t you just have a single function that can be done for comparisons?
Have the role IDs stored as constants (i.e. capitalised variables, not really constants, but still):
[lua]
COMBINE_COP = 1
COMBINE_TACTICAL = 2
COMBINE_ELITE = 3
COMBINE_BREACHER = 4
COMBINE_SERGEANT = 5
COMBINE_ENGORCER = 6
COMBINE_COMMANDER = 7
COMBINE_SNIPER = 8
COMBINE_JAILGUARD = 9
REBEL = 10

function metatab:GetRole()

return self:GetNWInt( "Role" )

end
– EG: ply:GetRole() == COMBINE_COP

function metatab:SaveRole()

self:SetPData( "Role", self:GetRole() )

end

function metatab:SetRole( role )

self:SetNWInt( "Role", role )
self:SaveRole() -- I'm not sure it's efficient to do this every time the role is changed
-- Try saving it on player disconnect and every 2.5 minutes or so instead

end
– EG: ply:SetRole( COMBINE_COP )
[/lua]
See how much cleaner that is?
Remember that everything but SetRole and SaveRole should be under a shared file, those two functions will only be able to be run server side.

Alternatively, you could just save the name of the role of the player as a networked string.
You can also create a table with the names of the team like so (client side, of course):
[lua]local names = {
COMBINE_COP = “Metro Cop”,
COMBINE_TACTICAL = “Combine Tactical”,
COMBINE_ELITE = “Combine Elite”,
COMBINE_BREACHER = “Combine Breacher”,
COMBINE_SERGEANT = “Combine Sergeant”,
COMBINE_ENFORCER = “Combine Enforcer”,
COMBINE_COMMANDER = “Combine Commander”,
COMBINE_SNIPER = “Combine Sniper”,
COMBINE_JAILGUARD = “Combine Jail Guard”,
REBEL = “Rebel”,
}

function metatab:RoleName()

return names[ self:GetRole() ]

end
– EG: ply:RoleName() would return “Metro Cop” if that was their role
[/lua]

Sorry for not helping you with your problem so far, but I think it’s important that you make your code neat for debugging and efficiency purposes.

[lua]hook.Add( “PostPlayerDraw”, “DrawPlayerInfo”, function( ply )
if not ply:Alive() then return end

local offset = Vector( 0, 0, 85 )
local offset2 = Vector( 0, 0, 80 )
local ang = LocalPlayer():EyeAngles()
local pos = ply:GetPos() +offset +ang:Up()
local pos2 = ply:GetPos() +offset2 +ang:Up()
ang:RotateAroundAxis( ang:Forward(), 90 )
ang:RotateAroundAxis( ang:Right(), 90 )

cam.Start3D2D( pos, Angle( 0, ang.y, 90 ), 0.25 )
	draw.DrawText( ply:GetName(), "BudgetLabel", 2, 2, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER )
cam.End3D2D()

cam.Start3D2D( pos2, Angle( 0, ang.y, 90 ), 0.25 )
	draw.DrawText( ply:RoleName(), "BudgetLabel", 2, 2, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER )
cam.End3D2D()

end )[/lua]

This should work for you, but if it doesn’t, then it’s something to do with your setting the player’s role, and you’ll have to post that.

Thanks so much!

And yeah, I knew there was probably a much more efficient way. Thanks for explaining what you did as well!