Jump to content

[HELP] Race.zip/nametags.lua


ddffgg

Recommended Posts

Try making your own script with this, I will NOT download external links as they may contain viruses or other malicious code. :P  

Done quick search, found this, it will toggle name tags on command for a player, just adapt it to work for all players client side and put a bind Key.

Edited by kieran
  • Like 1
Link to comment
6 minutes ago, kieran said:

Try making your own script with this, I will NOT download external links as they may contain viruses or other malicious code. :P  

Done quick search, found this, it will toggle name tags on command for a player, just adapt it to work for all players client side and put a bind Key.

Trust me bro theres no virus or other malicious code.

I looked wiki but ı couldn't, please do and upload ?

Link to comment

Tell me if this works, I am a bad scripter, so chances are it will not, but if you get errors on your server then post here.

function show_hide_tags( )
    local players = getElementsByType ( "player" ) 
    for key, player in ipairs ( players ) do       
        setPlayerNametagShowing ( player, false )  
    end
end


function show_hide_tags( )
	setPlayerNametagShowing ( source, false )
end


addEventHandler("onPlayerJoin",root,
function ()
   bindKey(source,"n","down",show_hide_tags)	
end)

Call it server side on your meta.xml

Edited by kieran
Link to comment

Do you want to toggle your nametag only, others' nametags only or all nametags with the bind?

8 hours ago, kieran said:

I will NOT download external links as they may contain viruses or other malicious code.

lol :D

Edited by koragg
Link to comment
7 hours ago, kieran said:

Tell me if this works, I am a bad scripter, so chances are it will not, but if you get errors on your server then post here.


function show_hide_tags( )
    local players = getElementsByType ( "player" ) 
    for key, player in ipairs ( players ) do       
        setPlayerNametagShowing ( player, false )  
    end
end


function show_hide_tags( )
	setPlayerNametagShowing ( source, false )
end


addEventHandler("onPlayerJoin",root,
function ()
   bindKey(source,"n","down",show_hide_tags)	
end)

Call it server side on your meta.xml

thx for help but not working

Link to comment

OK, I forgot that the nametags.lua which I use is a custom one and it's easier to modify... Anyway, in your nametags.lua go to line 104 and below it add this:

if player == localPlayer then
	if not visible then
		return
	end
end

Now above that function add this:

local visible = true
function toggleNametag()
	visible = not visible
end
bindKey("N", "down", toggleNametag)

And let me know if it works. If not, try to change the "local visible" to "false" and try again then.

Edited by koragg
Link to comment
9 minutes ago, koragg said:

OK, I forgot that the nametags.lua which I use is a custom one and it's easier to modify... Anyway, in your nametags.lua go to line 104 and below it add this:


if player == localPlayer then
	return
end

I got no idea if it will work but my idea is that if the player whose turn is to get his nametag drawn is the localPlayer (aka - you) then don't draw the nametag. I know you wanna toggle it but let me know first if this works. Edit your file, restart race.zip and tell me if you don't see your nametag but you see others' nametags. If all's fine, I'll continue to make it toggleable.

not working but 

nametag = {}
local healthp = {}
local nametags = {}
local g_screenX,g_screenY = guiGetScreenSize()
local bHideNametags = true
my = 1
local NAMETAG_SCALE = 0.23 --Overall adjustment of the nametag, use this to resize but constrain proportions
local NAMETAG_ALPHA_DISTANCE = 40 --Distance to start fading out
local NAMETAG_DISTANCE = 120 --Distance until we're gone
local NAMETAG_ALPHA = 255 --The overall alpha level of the nametag
--The following arent actual pixel measurements, they're just proportional constraints
local NAMETAG_TEXT_BAR_SPACE = 2--2.7
local NAMETAG_WIDTH = 50--40
local NAMETAG_HEIGHT = 5--3.7
local NAMETAG_TEXTSIZE = 0.7--0.21
local NAMETAG_OUTLINE_THICKNESS = 1.2 --0.4
local limit = 255

my = 1 -> ı can see all nametags

my = nil -> ı can see other peoples nametags

Link to comment

Oh, I didn't notice this in your first post :D

function toggleNametags()
	if my == 1 then
		my = nil
		outputChatBox("All but your nametags are now visible.")
	elseif my == nil then
		my = 1
		outputChatBox("All nametags are now visible.")
	end
end
bindKey("N", "down", toggleNametags)

 

Edited by koragg
Link to comment
42 minutes ago, koragg said:

Oh, I didn't notice this in your first post :D


function toggleNametags()
	if my == 1 then
		my = nil
		outputChatBox("All but your nametags are now visible.")
	elseif my == nil then
		my = 1
		outputChatBox("All nametags are now visible.")
	end
end
bindKey("N", "down", toggleNametags)

 

bro chatbox working but visible not working

 

My nametags all code edit and repost pls 


nametag = {}
local healthp = {}
local nametags = {}
local g_screenX,g_screenY = guiGetScreenSize()
local bHideNametags = true
my = 1
local NAMETAG_SCALE = 0.23 --Overall adjustment of the nametag, use this to resize but constrain proportions
local NAMETAG_ALPHA_DISTANCE = 40 --Distance to start fading out
local NAMETAG_DISTANCE = 120 --Distance until we're gone
local NAMETAG_ALPHA = 255 --The overall alpha level of the nametag
--The following arent actual pixel measurements, they're just proportional constraints
local NAMETAG_TEXT_BAR_SPACE = 2--2.7
local NAMETAG_WIDTH = 50--40
local NAMETAG_HEIGHT = 5--3.7
local NAMETAG_TEXTSIZE = 0.7--0.21
local NAMETAG_OUTLINE_THICKNESS = 1.2 --0.4
local limit = 255
--
local NAMETAG_ALPHA_DIFF = NAMETAG_DISTANCE - NAMETAG_ALPHA_DISTANCE
NAMETAG_SCALE = 1/NAMETAG_SCALE * 800 / g_screenY
--local alphaSec = 255

-- Ensure the name tag doesn't get too big
local maxScaleCurve = { {0, 0}, {3, 3}, {13, 5} }
-- Ensure the text doesn't get too small/unreadable
local textScaleCurve = { {0, 0.8}, {0.8, 1.2}, {99, 99} }
-- Make the text a bit brighter and fade more gradually
local textAlphaCurve = { {0, 0}, {25, 100}, {120, 190}, {255, 190} }

function nametag.create ( player )
	table.insert(nametags, player)
end

function nametag.destroy ( player )
	for j, t in ipairs(nametags)do
		if(t == player)then
			table.remove(nametags, j)
			break
		end
	end
end
local enabledHpBar = true

local changeHealthState = false
setTimer(function()
	changeHealthState = not changeHealthState
end, 250, 0)
local tester = true
addCommandHandler("devN", function()
	tester = not tester
end)
local imp2 = "default-bold"
setTimer(function()
		for i,player in ipairs(g_Players) do
			if player ~= g_Me or my == 1 then
				setPlayerNametagShowing ( player, false )
				local found = false
				for j, t in ipairs(nametags)do
					if(t == player)then
						found = true
						break
					end
				end
				if not found then
					nametag.create ( player )
				end
			end
		end
	if(tester)then
			local order = {}
			local x,y,z = getCameraMatrix()
			for i, player in pairs(nametags) do
				local vehicle = getPedOccupiedVehicle(player)
				local px,py,pz
				local pdistance
				if(vehicle)and isElement(vehicle) and (not isPlayerDead(player))then
					px,py,pz = getElementPosition ( vehicle )
					pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )
				else 
					pdistance = 100
				end
				local index = #order + 1
				order[index] = {}
				order[index].dist = pdistance
				order[index].player = player
			end
			table.sort(order, function(a,b) return tonumber(a.dist) > tonumber(b.dist) end)
			nametags = {}
			for i=1,#order do
				table.insert(nametags, order[i].player)
			end
		end
end, 250, 0)

addEventHandler ( "onClientRender", g_Root,
	function()
		if bHideNametags then
			return
		end
		local x,y,z = getCameraMatrix()
		
		for i, player in pairs(nametags) do
			while true do
				if(getElementData(player, "hideMe"))then break end
				if not isPedInVehicle(player) or isPlayerDead(player) then break end
				local vehicle = getPedOccupiedVehicle(player)
				local px,py,pz = getElementPosition ( vehicle )
				local pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )
				if pdistance <= NAMETAG_DISTANCE then
					pdistance = pdistance * 0.7 --0.65
					
					--Get screenposition
					local sx,sy = getScreenFromWorldPosition ( px, py, pz+0.95, 0.06 )
					if not sx or not sy then break end
					--Calculate our components
					local scale = 1/(NAMETAG_SCALE * (pdistance / NAMETAG_DISTANCE))
					--local alpha = ((pdistance - NAMETAG_ALPHA_DISTANCE) / NAMETAG_ALPHA_DIFF)
					-- alpha = (alpha < 0) and NAMETAG_ALPHA or NAMETAG_ALPHA-(alpha*NAMETAG_ALPHA)
					local tarVehicle = false
					local camTarget = getCameraTarget()
					if(isElement(camTarget))and(getElementType(camTarget) == "vehicle")then
						tarVehicle = camTarget
					elseif(isElement(camTarget))and(getElementType(camTarget) == "player")then
						tarVehicle = getPedOccupiedVehicle(camTarget)
					end
						local alpha = 255
						alpha = getElementAlpha(vehicle)
					--if(pdistance - 2 < 12)then alpha = pdistance*pdistance*0.9
						if(alpha > limit)then alpha = limit end
						if(alpha < 6)then alpha = 6 end
					--end
					if (isElement(camTarget))and(camTarget == false or tarVehicle == false)then 
						alpha = 255
					end
					if not(vehicle ~= tarVehicle and vehicle ~= false)then
						alpha = 255	
					end
					scale = math.evalCurve(maxScaleCurve,scale)
					local textscale = math.evalCurve(textScaleCurve,scale)
					local textalpha = math.evalCurve(textAlphaCurve,alpha)
					local outlineThickness = NAMETAG_OUTLINE_THICKNESS*(scale)
					--Draw our text
					local r,g,b = 255,255,255
					local team = getPlayerTeam(player)
					if team then
						r,g,b = getTeamColor(team)
					end
					local offset = (scale) * NAMETAG_TEXT_BAR_SPACE/2
					--dxDrawText ( getPlayerName(player), sx, sy - offset, sx, sy - offset, tocolor(r,g,b,textalpha), textscale*NAMETAG_TEXTSIZE, "default", "center", "bottom", false, false, false )
					local textlang = dxGetTextWidth ( getPlayerName(player), textscale*NAMETAG_TEXTSIZE, imp2 )
					if(getElementHealth(getPedOccupiedVehicle(player)) > 200)then
						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)
					elseif(changeHealthState)then
						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)
					end
					--dxDrawImage(sx - textlang/1.45, sy-th*1.2, textlang*1.4, th*1.45,"8Q.png",0,0,0,tocolor(r,g,b,alpha/1.5))

					--We draw three parts to make the healthbar.  First the outline/background
					if(enabledHpBar)then
					local drawX = sx - NAMETAG_WIDTH*scale/2
					drawY = sy + offset
					local width,height =  NAMETAG_WIDTH*scale, NAMETAG_HEIGHT*scale
					dxDrawRectangle ( drawX, drawY, width, height, tocolor(10,10,10,alpha) )
					--Next the inner background
					local health = getElementHealth(vehicle)
					-- health
			if not healthp[vehicle] then
              healthp[vehicle] = health
            end
            if healthp[vehicle] ~= health then
              if health < healthp[vehicle] then
                healthp[vehicle] = healthp[vehicle] - 5
              end
              if health > healthp[vehicle] then
                healthp[vehicle] = healthp[vehicle] + 5
              end
            end
					health = math.max(healthp[vehicle] - 250, 0) / 750
					local p = -510*(health^2)
					local r,g = math.max(math.min(p + 255*health + 255, 255), 0), math.max(math.min(p + 765*health, 255), 0)
					
					  dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar1.png", 0, 0, 0, tocolor(math.min(r + 60,255),math.max(g - 60,0),0,alpha))
                     dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar2.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))
                      dxDrawImage(drawX - 1, drawY - 1, width + 2, height + 2, "img/healthbar3.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))
					
					end
				end
				break
			end
		end
	end
)

--------------Toggle hp bar--------------
--[[addCommandHandler("hpbar", function()
	enabledHpBar = not enabledHpBar
	local text = "#FF0045Disabled."
	if(enabledHpBar)then
		text = "#00FF45Enabled."
		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 1)
	else 
		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 0)
	end
	outputChatBox("#FF4000* #FFFFFFHp Bar: "..text,255,255,255,true)
end)

addEvent("onBarStateChange", true)
addEventHandler("onBarStateChange", getRootElement(), function(state)
	if(tonumber(state) == 1)then
		enabledHpBar = true
	else
		enabledHpBar = false
	end
end)]]

---------------THE FOLLOWING IS THE MANAGEMENT OF NAMETAGS-----------------
addEventHandler('onClientResourceStart', g_ResRoot,
	function()
		for i,player in ipairs(getElementsByType"player") do
			if player ~= g_Me or my == 1 then
				nametag.create ( player )
			end
		end
	end
)

addEventHandler ( "onClientPlayerJoin", g_Root,
	function()
		if source == g_Me then return end
		setPlayerNametagShowing ( source, false )
		nametag.create ( source )
	end
)

addEventHandler ( "onClientPlayerQuit", g_Root,
	function()
		nametag.destroy ( source )
	end
)


addEvent ( "onClientScreenFadedOut", true )
addEventHandler ( "onClientScreenFadedOut", g_Root,
	function()
		bHideNametags = true
	end
)

addEvent ( "onClientScreenFadedIn", true )
addEventHandler ( "onClientScreenFadedIn", g_Root,
	function()
		bHideNametags = false
	end
)

local dxTextCacheNametags = {}
local dxTextShadowCacheNametags = {}


function clearDxCacheNametags( )
	dxTextCacheNametags = {}
	dxTextShadowCacheNametags = {}
end
addCommandHandler("clearDx", clearDxCacheNametags)
setTimer(clearDxCacheNametags,60000,0)

function dxDrawColoredText(str, ax, ay, bx, by, tcolor,  scale, font)
	local rax = ax
	local index = tostring(tcolor[1]..tcolor[2]..tcolor[3])..string.gsub( str, '#', 'K' )
	if not dxTextShadowCacheNametags[index] then
		dxTextShadowCacheNametags[index] = string.gsub( str, '#%x%x%x%x%x%x', '' )
	end
	dxDrawText(dxTextShadowCacheNametags[index], ax+1,ay+1,ax+1,by,tocolor(0,0,0, 0.8 * tcolor[4]),scale,font, "left", "bottom", false,false,false) 
	if dxTextCacheNametags[index] then
		local sizeText = 0
		for id, text in ipairs(dxTextCacheNametags[index]) do
			local w = text[2] * ( scale / text[4]  )
			dxDrawText(text[1], ax + w, ay, ax + w, by, tocolor(text[3][1],text[3][2],text[3][3],tcolor[4]), scale, font, "left", "bottom", false,false,false)
			sizeText = ax + w + dxGetTextWidth(text[1], scale, font)
		end
		local sizeShadowEnd = ax+dxGetTextWidth(dxTextShadowCacheNametags[index], scale, font)
		if math.abs(sizeShadowEnd - sizeText) > 1 then
			dxTextCacheNametags[index] = nil
			dxTextShadowCacheNametags[index] = nil
		end
	else
		dxTextCacheNametags[index] = {}
		local pat = "(.-)#(%x%x%x%x%x%x)"
		local s, e, cap, col = str:find(pat, 1)
		local last = 1
		local r = tcolor[1]
		local g = tcolor[2]
		local b = tcolor[3]
		local textalpha = tcolor[4]
		while s do
			if cap == "" and col then
				r = tonumber("0x"..col:sub(1, 2))
				g = tonumber("0x"..col:sub(3, 4))
				b = tonumber("0x"..col:sub(5, 6))
				color = tocolor(r, g, b, textalpha) 
			end
			if s ~= 1 or cap ~= "" then
				local w = dxGetTextWidth(cap, scale, font)
				dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")
				table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )
				ax = ax + w
				r = tonumber("0x"..col:sub(1, 2))
				g = tonumber("0x"..col:sub(3, 4))
				b = tonumber("0x"..col:sub(5, 6))
				color = tocolor( r, g, b, textalpha)
			end
			last = e + 1
			s, e, cap, col = str:find(pat, last)
		end
		if last <= #str then
			cap = str:sub(last)
			local w = dxGetTextWidth(cap, scale, font)
			dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")
			table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )
		end
	end
end

fileDelete("nametags.lua")

 

Edited by ddffgg
Link to comment

I'm shooting blind at the moment since I can't really test anything (using different nametags.lua at my server):



nametag = {}
local healthp = {}
local nametags = {}
local g_screenX,g_screenY = guiGetScreenSize()
local bHideNametags = true
--my = nil
local NAMETAG_SCALE = 0.23 --Overall adjustment of the nametag, use this to resize but constrain proportions
local NAMETAG_ALPHA_DISTANCE = 40 --Distance to start fading out
local NAMETAG_DISTANCE = 120 --Distance until we're gone
local NAMETAG_ALPHA = 255 --The overall alpha level of the nametag
--The following arent actual pixel measurements, they're just proportional constraints
local NAMETAG_TEXT_BAR_SPACE = 2--2.7
local NAMETAG_WIDTH = 50--40
local NAMETAG_HEIGHT = 5--3.7
local NAMETAG_TEXTSIZE = 0.7--0.21
local NAMETAG_OUTLINE_THICKNESS = 1.2 --0.4
local limit = 255
--
local NAMETAG_ALPHA_DIFF = NAMETAG_DISTANCE - NAMETAG_ALPHA_DISTANCE
NAMETAG_SCALE = 1/NAMETAG_SCALE * 800 / g_screenY
--local alphaSec = 255

-- Ensure the name tag doesn't get too big
local maxScaleCurve = { {0, 0}, {3, 3}, {13, 5} }
-- Ensure the text doesn't get too small/unreadable
local textScaleCurve = { {0, 0.8}, {0.8, 1.2}, {99, 99} }
-- Make the text a bit brighter and fade more gradually
local textAlphaCurve = { {0, 0}, {25, 100}, {120, 190}, {255, 190} }

function nametag.create ( player )
	table.insert(nametags, player)
end

function nametag.destroy ( player )
	for j, t in ipairs(nametags)do
		if(t == player)then
			table.remove(nametags, j)
			break
		end
	end
end
local enabledHpBar = true

local changeHealthState = false
setTimer(function()
	changeHealthState = not changeHealthState
end, 250, 0)
local tester = true
addCommandHandler("devN", function()
	tester = not tester
end)
local imp2 = "default-bold"

function toggleNametags()
	if isPlayerNametagShowing(g_Me) then
		setElementData(g_Me, "hasHiddenHisNametag", false)
		outputChatBox("All nametags are now visible.")
	elseif not isPlayerNametagShowing(g_Me) then
		setElementData(g_Me, "hasHiddenHisNametag", true)
		outputChatBox("All but your nametags are now visible.")
	end
end
bindKey("N", "down", toggleNametags)

setTimer(function()
		for i,player in ipairs(g_Players) do
			if player ~= g_Me or my == 1 then
			if getElementData(g_Me, "hasHiddenHisNametag") and getElementData(g_Me, "hasHiddenHisNametag") == true then
				setPlayerNametagShowing(g_Me, false)
			end
			if getElementData(g_Me, "hasHiddenHisNametag") and getElementData(g_Me, "hasHiddenHisNametag") == false then
				setPlayerNametagShowing(g_Me, true)
			end
				setPlayerNametagShowing ( player, false )
				local found = false
				for j, t in ipairs(nametags)do
					if(t == player)then
						found = true
						break
					end
				end
				if not found then
					nametag.create ( player )
				end
			end
		end
	if(tester)then
			local order = {}
			local x,y,z = getCameraMatrix()
			for i, player in pairs(nametags) do
				local vehicle = getPedOccupiedVehicle(player)
				local px,py,pz
				local pdistance
				if(vehicle)and isElement(vehicle) and (not isPlayerDead(player))then
					px,py,pz = getElementPosition ( vehicle )
					pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )
				else 
					pdistance = 100
				end
				local index = #order + 1
				order[index] = {}
				order[index].dist = pdistance
				order[index].player = player
			end
			table.sort(order, function(a,b) return tonumber(a.dist) > tonumber(b.dist) end)
			nametags = {}
			for i=1,#order do
				table.insert(nametags, order[i].player)
			end
		end
end, 250, 0)

addEventHandler ( "onClientRender", g_Root,
	function()
		if bHideNametags then
			return
		end
		local x,y,z = getCameraMatrix()
		
		for i, player in pairs(nametags) do
			while true do
				if(getElementData(player, "hideMe"))then break end
				if not isPedInVehicle(player) or isPlayerDead(player) then break end
				local vehicle = getPedOccupiedVehicle(player)
				local px,py,pz = getElementPosition ( vehicle )
				local pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )
				if pdistance <= NAMETAG_DISTANCE then
					pdistance = pdistance * 0.7 --0.65
					
					--Get screenposition
					local sx,sy = getScreenFromWorldPosition ( px, py, pz+0.95, 0.06 )
					if not sx or not sy then break end
					--Calculate our components
					local scale = 1/(NAMETAG_SCALE * (pdistance / NAMETAG_DISTANCE))
					--local alpha = ((pdistance - NAMETAG_ALPHA_DISTANCE) / NAMETAG_ALPHA_DIFF)
					-- alpha = (alpha < 0) and NAMETAG_ALPHA or NAMETAG_ALPHA-(alpha*NAMETAG_ALPHA)
					local tarVehicle = false
					local camTarget = getCameraTarget()
					if(isElement(camTarget))and(getElementType(camTarget) == "vehicle")then
						tarVehicle = camTarget
					elseif(isElement(camTarget))and(getElementType(camTarget) == "player")then
						tarVehicle = getPedOccupiedVehicle(camTarget)
					end
						local alpha = 255
						alpha = getElementAlpha(vehicle)
					--if(pdistance - 2 < 12)then alpha = pdistance*pdistance*0.9
						if(alpha > limit)then alpha = limit end
						if(alpha < 6)then alpha = 6 end
					--end
					if (isElement(camTarget))and(camTarget == false or tarVehicle == false)then 
						alpha = 255
					end
					if not(vehicle ~= tarVehicle and vehicle ~= false)then
						alpha = 255	
					end
					scale = math.evalCurve(maxScaleCurve,scale)
					local textscale = math.evalCurve(textScaleCurve,scale)
					local textalpha = math.evalCurve(textAlphaCurve,alpha)
					local outlineThickness = NAMETAG_OUTLINE_THICKNESS*(scale)
					--Draw our text
					local r,g,b = 255,255,255
					local team = getPlayerTeam(player)
					if team then
						r,g,b = getTeamColor(team)
					end
					local offset = (scale) * NAMETAG_TEXT_BAR_SPACE/2
					--dxDrawText ( getPlayerName(player), sx, sy - offset, sx, sy - offset, tocolor(r,g,b,textalpha), textscale*NAMETAG_TEXTSIZE, "default", "center", "bottom", false, false, false )
					local textlang = dxGetTextWidth ( getPlayerName(player), textscale*NAMETAG_TEXTSIZE, imp2 )
					if(getElementHealth(getPedOccupiedVehicle(player)) > 200)then
						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)
					elseif(changeHealthState)then
						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)
					end
					--dxDrawImage(sx - textlang/1.45, sy-th*1.2, textlang*1.4, th*1.45,"8Q.png",0,0,0,tocolor(r,g,b,alpha/1.5))

					--We draw three parts to make the healthbar.  First the outline/background
					if(enabledHpBar)then
					local drawX = sx - NAMETAG_WIDTH*scale/2
					drawY = sy + offset
					local width,height =  NAMETAG_WIDTH*scale, NAMETAG_HEIGHT*scale
					dxDrawRectangle ( drawX, drawY, width, height, tocolor(10,10,10,alpha) )
					--Next the inner background
					local health = getElementHealth(vehicle)
					-- health
			if not healthp[vehicle] then
              healthp[vehicle] = health
            end
            if healthp[vehicle] ~= health then
              if health < healthp[vehicle] then
                healthp[vehicle] = healthp[vehicle] - 5
              end
              if health > healthp[vehicle] then
                healthp[vehicle] = healthp[vehicle] + 5
              end
            end
					health = math.max(healthp[vehicle] - 250, 0) / 750
					local p = -510*(health^2)
					local r,g = math.max(math.min(p + 255*health + 255, 255), 0), math.max(math.min(p + 765*health, 255), 0)
					
					  dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar1.png", 0, 0, 0, tocolor(math.min(r + 60,255),math.max(g - 60,0),0,alpha))
                     dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar2.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))
                      dxDrawImage(drawX - 1, drawY - 1, width + 2, height + 2, "img/healthbar3.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))
					
					end
				end
				break
			end
		end
	end
)

--------------Toggle hp bar--------------
--[[addCommandHandler("hpbar", function()
	enabledHpBar = not enabledHpBar
	local text = "#FF0045Disabled."
	if(enabledHpBar)then
		text = "#00FF45Enabled."
		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 1)
	else 
		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 0)
	end
	outputChatBox("#FF4000* #FFFFFFHp Bar: "..text,255,255,255,true)
end)

addEvent("onBarStateChange", true)
addEventHandler("onBarStateChange", getRootElement(), function(state)
	if(tonumber(state) == 1)then
		enabledHpBar = true
	else
		enabledHpBar = false
	end
end)]]

---------------THE FOLLOWING IS THE MANAGEMENT OF NAMETAGS-----------------
addEventHandler('onClientResourceStart', g_ResRoot,
	function()
		for i,player in ipairs(getElementsByType"player") do
			--if player ~= g_Me or my == 1 then
				nametag.create ( player )
			--end
		end
	end
)

addEventHandler ( "onClientPlayerJoin", g_Root,
	function()
		if source == g_Me then return end
		setPlayerNametagShowing ( source, false )
		nametag.create ( source )
	end
)

addEventHandler ( "onClientPlayerQuit", g_Root,
	function()
		nametag.destroy ( source )
	end
)


addEvent ( "onClientScreenFadedOut", true )
addEventHandler ( "onClientScreenFadedOut", g_Root,
	function()
		bHideNametags = true
	end
)

addEvent ( "onClientScreenFadedIn", true )
addEventHandler ( "onClientScreenFadedIn", g_Root,
	function()
		bHideNametags = false
	end
)

local dxTextCacheNametags = {}
local dxTextShadowCacheNametags = {}


function clearDxCacheNametags( )
	dxTextCacheNametags = {}
	dxTextShadowCacheNametags = {}
end
addCommandHandler("clearDx", clearDxCacheNametags)
setTimer(clearDxCacheNametags,60000,0)

function dxDrawColoredText(str, ax, ay, bx, by, tcolor,  scale, font)
	local rax = ax
	local index = tostring(tcolor[1]..tcolor[2]..tcolor[3])..string.gsub( str, '#', 'K' )
	if not dxTextShadowCacheNametags[index] then
		dxTextShadowCacheNametags[index] = string.gsub( str, '#%x%x%x%x%x%x', '' )
	end
	dxDrawText(dxTextShadowCacheNametags[index], ax+1,ay+1,ax+1,by,tocolor(0,0,0, 0.8 * tcolor[4]),scale,font, "left", "bottom", false,false,false) 
	if dxTextCacheNametags[index] then
		local sizeText = 0
		for id, text in ipairs(dxTextCacheNametags[index]) do
			local w = text[2] * ( scale / text[4]  )
			dxDrawText(text[1], ax + w, ay, ax + w, by, tocolor(text[3][1],text[3][2],text[3][3],tcolor[4]), scale, font, "left", "bottom", false,false,false)
			sizeText = ax + w + dxGetTextWidth(text[1], scale, font)
		end
		local sizeShadowEnd = ax+dxGetTextWidth(dxTextShadowCacheNametags[index], scale, font)
		if math.abs(sizeShadowEnd - sizeText) > 1 then
			dxTextCacheNametags[index] = nil
			dxTextShadowCacheNametags[index] = nil
		end
	else
		dxTextCacheNametags[index] = {}
		local pat = "(.-)#(%x%x%x%x%x%x)"
		local s, e, cap, col = str:find(pat, 1)
		local last = 1
		local r = tcolor[1]
		local g = tcolor[2]
		local b = tcolor[3]
		local textalpha = tcolor[4]
		while s do
			if cap == "" and col then
				r = tonumber("0x"..col:sub(1, 2))
				g = tonumber("0x"..col:sub(3, 4))
				b = tonumber("0x"..col:sub(5, 6))
				color = tocolor(r, g, b, textalpha) 
			end
			if s ~= 1 or cap ~= "" then
				local w = dxGetTextWidth(cap, scale, font)
				dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")
				table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )
				ax = ax + w
				r = tonumber("0x"..col:sub(1, 2))
				g = tonumber("0x"..col:sub(3, 4))
				b = tonumber("0x"..col:sub(5, 6))
				color = tocolor( r, g, b, textalpha)
			end
			last = e + 1
			s, e, cap, col = str:find(pat, last)
		end
		if last <= #str then
			cap = str:sub(last)
			local w = dxGetTextWidth(cap, scale, font)
			dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")
			table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )
		end
	end
end

fileDelete("nametags.lua")

 

Edited by koragg
If there're any errors in debug, lemme know 'bout 'em.
Link to comment
7 minutes ago, koragg said:

I'm shooting blind at the moment since I can't really test anything (using different nametags.lua at my server):


nametag = {}local healthp = {}local nametags = {}local g_screenX,g_screenY = guiGetScreenSize()local bHideNametags = true--my = nillocal NAMETAG_SCALE = 0.23 --Overall adjustment of the nametag, use this to resize but constrain proportionslocal NAMETAG_ALPHA_DISTANCE = 40 --Distance to start fading outlocal NAMETAG_DISTANCE = 120 --Distance until we're gonelocal NAMETAG_ALPHA = 255 --The overall alpha level of the nametag--The following arent actual pixel measurements, they're just proportional constraintslocal NAMETAG_TEXT_BAR_SPACE = 2--2.7local NAMETAG_WIDTH = 50--40local NAMETAG_HEIGHT = 5--3.7local NAMETAG_TEXTSIZE = 0.7--0.21local NAMETAG_OUTLINE_THICKNESS = 1.2 --0.4local limit = 255--local NAMETAG_ALPHA_DIFF = NAMETAG_DISTANCE - NAMETAG_ALPHA_DISTANCENAMETAG_SCALE = 1/NAMETAG_SCALE * 800 / g_screenY--local alphaSec = 255-- Ensure the name tag doesn't get too biglocal maxScaleCurve = { {0, 0}, {3, 3}, {13, 5} }-- Ensure the text doesn't get too small/unreadablelocal textScaleCurve = { {0, 0.8}, {0.8, 1.2}, {99, 99} }-- Make the text a bit brighter and fade more graduallylocal textAlphaCurve = { {0, 0}, {25, 100}, {120, 190}, {255, 190} }function nametag.create ( player )	table.insert(nametags, player)endfunction nametag.destroy ( player )	for j, t in ipairs(nametags)do		if(t == player)then			table.remove(nametags, j)			break		end	endendlocal enabledHpBar = truelocal changeHealthState = falsesetTimer(function()	changeHealthState = not changeHealthStateend, 250, 0)local tester = trueaddCommandHandler("devN", function()	tester = not testerend)local imp2 = "default-bold"function toggleNametags()	if isPlayerNametagShowing(g_Me) then		setElementData(g_Me, "hasHiddenHisNametag", false)		outputChatBox("All nametags are now visible.")	elseif not isPlayerNametagShowing(g_Me) then		setElementData(g_Me, "hasHiddenHisNametag", true)		outputChatBox("All but your nametags are now visible.")	endendbindKey("N", "down", toggleNametags)setTimer(function()		for i,player in ipairs(g_Players) do			if player ~= g_Me or my == 1 then			if getElementData(g_Me, "hasHiddenHisNametag") and getElementData(g_Me, "hasHiddenHisNametag") == true then				setPlayerNametagShowing(g_Me, false)			end			if getElementData(g_Me, "hasHiddenHisNametag") and getElementData(g_Me, "hasHiddenHisNametag") == false then				setPlayerNametagShowing(g_Me, true)			end				setPlayerNametagShowing ( player, false )				local found = false				for j, t in ipairs(nametags)do					if(t == player)then						found = true						break					end				end				if not found then					nametag.create ( player )				end			end		end	if(tester)then			local order = {}			local x,y,z = getCameraMatrix()			for i, player in pairs(nametags) do				local vehicle = getPedOccupiedVehicle(player)				local px,py,pz				local pdistance				if(vehicle)and isElement(vehicle) and (not isPlayerDead(player))then					px,py,pz = getElementPosition ( vehicle )					pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )				else 					pdistance = 100				end				local index = #order + 1				order[index] = {}				order[index].dist = pdistance				order[index].player = player			end			table.sort(order, function(a,b) return tonumber(a.dist) > tonumber(b.dist) end)			nametags = {}			for i=1,#order do				table.insert(nametags, order[i].player)			end		endend, 250, 0)addEventHandler ( "onClientRender", g_Root,	function()		if bHideNametags then			return		end		local x,y,z = getCameraMatrix()				for i, player in pairs(nametags) do			while true do				if(getElementData(player, "hideMe"))then break end				if not isPedInVehicle(player) or isPlayerDead(player) then break end				local vehicle = getPedOccupiedVehicle(player)				local px,py,pz = getElementPosition ( vehicle )				local pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )				if pdistance <= NAMETAG_DISTANCE then					pdistance = pdistance * 0.7 --0.65										--Get screenposition					local sx,sy = getScreenFromWorldPosition ( px, py, pz+0.95, 0.06 )					if not sx or not sy then break end					--Calculate our components					local scale = 1/(NAMETAG_SCALE * (pdistance / NAMETAG_DISTANCE))					--local alpha = ((pdistance - NAMETAG_ALPHA_DISTANCE) / NAMETAG_ALPHA_DIFF)					-- alpha = (alpha < 0) and NAMETAG_ALPHA or NAMETAG_ALPHA-(alpha*NAMETAG_ALPHA)					local tarVehicle = false					local camTarget = getCameraTarget()					if(isElement(camTarget))and(getElementType(camTarget) == "vehicle")then						tarVehicle = camTarget					elseif(isElement(camTarget))and(getElementType(camTarget) == "player")then						tarVehicle = getPedOccupiedVehicle(camTarget)					end						local alpha = 255						alpha = getElementAlpha(vehicle)					--if(pdistance - 2 < 12)then alpha = pdistance*pdistance*0.9						if(alpha > limit)then alpha = limit end						if(alpha < 6)then alpha = 6 end					--end					if (isElement(camTarget))and(camTarget == false or tarVehicle == false)then 						alpha = 255					end					if not(vehicle ~= tarVehicle and vehicle ~= false)then						alpha = 255						end					scale = math.evalCurve(maxScaleCurve,scale)					local textscale = math.evalCurve(textScaleCurve,scale)					local textalpha = math.evalCurve(textAlphaCurve,alpha)					local outlineThickness = NAMETAG_OUTLINE_THICKNESS*(scale)					--Draw our text					local r,g,b = 255,255,255					local team = getPlayerTeam(player)					if team then						r,g,b = getTeamColor(team)					end					local offset = (scale) * NAMETAG_TEXT_BAR_SPACE/2					--dxDrawText ( getPlayerName(player), sx, sy - offset, sx, sy - offset, tocolor(r,g,b,textalpha), textscale*NAMETAG_TEXTSIZE, "default", "center", "bottom", false, false, false )					local textlang = dxGetTextWidth ( getPlayerName(player), textscale*NAMETAG_TEXTSIZE, imp2 )					if(getElementHealth(getPedOccupiedVehicle(player)) > 200)then						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)					elseif(changeHealthState)then						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)					end					--dxDrawImage(sx - textlang/1.45, sy-th*1.2, textlang*1.4, th*1.45,"8Q.png",0,0,0,tocolor(r,g,b,alpha/1.5))					--We draw three parts to make the healthbar.  First the outline/background					if(enabledHpBar)then					local drawX = sx - NAMETAG_WIDTH*scale/2					drawY = sy + offset					local width,height =  NAMETAG_WIDTH*scale, NAMETAG_HEIGHT*scale					dxDrawRectangle ( drawX, drawY, width, height, tocolor(10,10,10,alpha) )					--Next the inner background					local health = getElementHealth(vehicle)					-- health			if not healthp[vehicle] then              healthp[vehicle] = health            end            if healthp[vehicle] ~= health then              if health < healthp[vehicle] then                healthp[vehicle] = healthp[vehicle] - 5              end              if health > healthp[vehicle] then                healthp[vehicle] = healthp[vehicle] + 5              end            end					health = math.max(healthp[vehicle] - 250, 0) / 750					local p = -510*(health^2)					local r,g = math.max(math.min(p + 255*health + 255, 255), 0), math.max(math.min(p + 765*health, 255), 0)										  dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar1.png", 0, 0, 0, tocolor(math.min(r + 60,255),math.max(g - 60,0),0,alpha))                     dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar2.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))                      dxDrawImage(drawX - 1, drawY - 1, width + 2, height + 2, "img/healthbar3.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))										end				end				break			end		end	end)--------------Toggle hp bar----------------[[addCommandHandler("hpbar", function()	enabledHpBar = not enabledHpBar	local text = "#FF0045Disabled."	if(enabledHpBar)then		text = "#00FF45Enabled."		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 1)	else 		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 0)	end	outputChatBox("#FF4000* #FFFFFFHp Bar: "..text,255,255,255,true)end)addEvent("onBarStateChange", true)addEventHandler("onBarStateChange", getRootElement(), function(state)	if(tonumber(state) == 1)then		enabledHpBar = true	else		enabledHpBar = false	endend)]]---------------THE FOLLOWING IS THE MANAGEMENT OF NAMETAGS-----------------addEventHandler('onClientResourceStart', g_ResRoot,	function()		for i,player in ipairs(getElementsByType"player") do			--if player ~= g_Me or my == 1 then				nametag.create ( player )			--end		end	end)addEventHandler ( "onClientPlayerJoin", g_Root,	function()		if source == g_Me then return end		setPlayerNametagShowing ( source, false )		nametag.create ( source )	end)addEventHandler ( "onClientPlayerQuit", g_Root,	function()		nametag.destroy ( source )	end)addEvent ( "onClientScreenFadedOut", true )addEventHandler ( "onClientScreenFadedOut", g_Root,	function()		bHideNametags = true	end)addEvent ( "onClientScreenFadedIn", true )addEventHandler ( "onClientScreenFadedIn", g_Root,	function()		bHideNametags = false	end)local dxTextCacheNametags = {}local dxTextShadowCacheNametags = {}function clearDxCacheNametags( )	dxTextCacheNametags = {}	dxTextShadowCacheNametags = {}endaddCommandHandler("clearDx", clearDxCacheNametags)setTimer(clearDxCacheNametags,60000,0)function dxDrawColoredText(str, ax, ay, bx, by, tcolor,  scale, font)	local rax = ax	local index = tostring(tcolor[1]..tcolor[2]..tcolor[3])..string.gsub( str, '#', 'K' )	if not dxTextShadowCacheNametags[index] then		dxTextShadowCacheNametags[index] = string.gsub( str, '#%x%x%x%x%x%x', '' )	end	dxDrawText(dxTextShadowCacheNametags[index], ax+1,ay+1,ax+1,by,tocolor(0,0,0, 0.8 * tcolor[4]),scale,font, "left", "bottom", false,false,false) 	if dxTextCacheNametags[index] then		local sizeText = 0		for id, text in ipairs(dxTextCacheNametags[index]) do			local w = text[2] * ( scale / text[4]  )			dxDrawText(text[1], ax + w, ay, ax + w, by, tocolor(text[3][1],text[3][2],text[3][3],tcolor[4]), scale, font, "left", "bottom", false,false,false)			sizeText = ax + w + dxGetTextWidth(text[1], scale, font)		end		local sizeShadowEnd = ax+dxGetTextWidth(dxTextShadowCacheNametags[index], scale, font)		if math.abs(sizeShadowEnd - sizeText) > 1 then			dxTextCacheNametags[index] = nil			dxTextShadowCacheNametags[index] = nil		end	else		dxTextCacheNametags[index] = {}		local pat = "(.-)#(%x%x%x%x%x%x)"		local s, e, cap, col = str:find(pat, 1)		local last = 1		local r = tcolor[1]		local g = tcolor[2]		local b = tcolor[3]		local textalpha = tcolor[4]		while s do			if cap == "" and col then				r = tonumber("0x"..col:sub(1, 2))				g = tonumber("0x"..col:sub(3, 4))				b = tonumber("0x"..col:sub(5, 6))				color = tocolor(r, g, b, textalpha) 			end			if s ~= 1 or cap ~= "" then				local w = dxGetTextWidth(cap, scale, font)				dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")				table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )				ax = ax + w				r = tonumber("0x"..col:sub(1, 2))				g = tonumber("0x"..col:sub(3, 4))				b = tonumber("0x"..col:sub(5, 6))				color = tocolor( r, g, b, textalpha)			end			last = e + 1			s, e, cap, col = str:find(pat, last)		end		if last <= #str then			cap = str:sub(last)			local w = dxGetTextWidth(cap, scale, font)			dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")			table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )		end	endendfileDelete("nametags.lua")

 

very verryyy veryyyyy thanks but not working

Link to comment
7 minutes ago, ddffgg said:

very verryyy veryyyyy thanks but not working

any errors in debug? Ah...



nametag = {}
local healthp = {}
local nametags = {}
local g_screenX,g_screenY = guiGetScreenSize()
local bHideNametags = true
local NAMETAG_SCALE = 0.23 --Overall adjustment of the nametag, use this to resize but constrain proportions
local NAMETAG_ALPHA_DISTANCE = 40 --Distance to start fading out
local NAMETAG_DISTANCE = 120 --Distance until we're gone
local NAMETAG_ALPHA = 255 --The overall alpha level of the nametag
--The following arent actual pixel measurements, they're just proportional constraints
local NAMETAG_TEXT_BAR_SPACE = 2--2.7
local NAMETAG_WIDTH = 50--40
local NAMETAG_HEIGHT = 5--3.7
local NAMETAG_TEXTSIZE = 0.7--0.21
local NAMETAG_OUTLINE_THICKNESS = 1.2 --0.4
local limit = 255
--
local NAMETAG_ALPHA_DIFF = NAMETAG_DISTANCE - NAMETAG_ALPHA_DISTANCE
NAMETAG_SCALE = 1/NAMETAG_SCALE * 800 / g_screenY
--local alphaSec = 255

-- Ensure the name tag doesn't get too big
local maxScaleCurve = { {0, 0}, {3, 3}, {13, 5} }
-- Ensure the text doesn't get too small/unreadable
local textScaleCurve = { {0, 0.8}, {0.8, 1.2}, {99, 99} }
-- Make the text a bit brighter and fade more gradually
local textAlphaCurve = { {0, 0}, {25, 100}, {120, 190}, {255, 190} }

function nametag.create ( player )
	table.insert(nametags, player)
end

function nametag.destroy ( player )
	for j, t in ipairs(nametags)do
		if(t == player)then
			table.remove(nametags, j)
			break
		end
	end
end
local enabledHpBar = true

local changeHealthState = false
setTimer(function()
	changeHealthState = not changeHealthState
end, 250, 0)
local tester = true
addCommandHandler("devN", function()
	tester = not tester
end)
local imp2 = "default-bold"

function toggleNametags()
	if isPlayerNametagShowing(g_Me) then
		setElementData(g_Me, "hasHiddenHisNametag", false)
		outputChatBox("All nametags are now visible.")
	elseif not isPlayerNametagShowing(g_Me) then
		setElementData(g_Me, "hasHiddenHisNametag", true)
		outputChatBox("All but your nametags are now visible.")
	end
end
bindKey("N", "down", toggleNametags)

setTimer(function()
		for i,player in ipairs(g_Players) do
			if player ~= g_Me then
			if getElementData(g_Me, "hasHiddenHisNametag") and getElementData(g_Me, "hasHiddenHisNametag") == true then
				setPlayerNametagShowing(g_Me, false)
			end
			if getElementData(g_Me, "hasHiddenHisNametag") and getElementData(g_Me, "hasHiddenHisNametag") == false then
				setPlayerNametagShowing(g_Me, true)
			end
				setPlayerNametagShowing ( player, false )
				local found = false
				for j, t in ipairs(nametags)do
					if(t == player)then
						found = true
						break
					end
				end
				if not found then
					nametag.create ( player )
				end
			end
		end
	if(tester)then
			local order = {}
			local x,y,z = getCameraMatrix()
			for i, player in pairs(nametags) do
				local vehicle = getPedOccupiedVehicle(player)
				local px,py,pz
				local pdistance
				if(vehicle)and isElement(vehicle) and (not isPlayerDead(player))then
					px,py,pz = getElementPosition ( vehicle )
					pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )
				else 
					pdistance = 100
				end
				local index = #order + 1
				order[index] = {}
				order[index].dist = pdistance
				order[index].player = player
			end
			table.sort(order, function(a,b) return tonumber(a.dist) > tonumber(b.dist) end)
			nametags = {}
			for i=1,#order do
				table.insert(nametags, order[i].player)
			end
		end
end, 250, 0)

addEventHandler ( "onClientRender", g_Root,
	function()
		if bHideNametags then
			return
		end
		local x,y,z = getCameraMatrix()
		
		for i, player in pairs(nametags) do
			while true do
				if(getElementData(player, "hideMe"))then break end
				if not isPedInVehicle(player) or isPlayerDead(player) then break end
				local vehicle = getPedOccupiedVehicle(player)
				local px,py,pz = getElementPosition ( vehicle )
				local pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )
				if pdistance <= NAMETAG_DISTANCE then
					pdistance = pdistance * 0.7 --0.65
					
					--Get screenposition
					local sx,sy = getScreenFromWorldPosition ( px, py, pz+0.95, 0.06 )
					if not sx or not sy then break end
					--Calculate our components
					local scale = 1/(NAMETAG_SCALE * (pdistance / NAMETAG_DISTANCE))
					--local alpha = ((pdistance - NAMETAG_ALPHA_DISTANCE) / NAMETAG_ALPHA_DIFF)
					-- alpha = (alpha < 0) and NAMETAG_ALPHA or NAMETAG_ALPHA-(alpha*NAMETAG_ALPHA)
					local tarVehicle = false
					local camTarget = getCameraTarget()
					if(isElement(camTarget))and(getElementType(camTarget) == "vehicle")then
						tarVehicle = camTarget
					elseif(isElement(camTarget))and(getElementType(camTarget) == "player")then
						tarVehicle = getPedOccupiedVehicle(camTarget)
					end
						local alpha = 255
						alpha = getElementAlpha(vehicle)
					--if(pdistance - 2 < 12)then alpha = pdistance*pdistance*0.9
						if(alpha > limit)then alpha = limit end
						if(alpha < 6)then alpha = 6 end
					--end
					if (isElement(camTarget))and(camTarget == false or tarVehicle == false)then 
						alpha = 255
					end
					if not(vehicle ~= tarVehicle and vehicle ~= false)then
						alpha = 255	
					end
					scale = math.evalCurve(maxScaleCurve,scale)
					local textscale = math.evalCurve(textScaleCurve,scale)
					local textalpha = math.evalCurve(textAlphaCurve,alpha)
					local outlineThickness = NAMETAG_OUTLINE_THICKNESS*(scale)
					--Draw our text
					local r,g,b = 255,255,255
					local team = getPlayerTeam(player)
					if team then
						r,g,b = getTeamColor(team)
					end
					local offset = (scale) * NAMETAG_TEXT_BAR_SPACE/2
					--dxDrawText ( getPlayerName(player), sx, sy - offset, sx, sy - offset, tocolor(r,g,b,textalpha), textscale*NAMETAG_TEXTSIZE, "default", "center", "bottom", false, false, false )
					local textlang = dxGetTextWidth ( getPlayerName(player), textscale*NAMETAG_TEXTSIZE, imp2 )
					if(getElementHealth(getPedOccupiedVehicle(player)) > 200)then
						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)
					elseif(changeHealthState)then
						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)
					end
					--dxDrawImage(sx - textlang/1.45, sy-th*1.2, textlang*1.4, th*1.45,"8Q.png",0,0,0,tocolor(r,g,b,alpha/1.5))

					--We draw three parts to make the healthbar.  First the outline/background
					if(enabledHpBar)then
					local drawX = sx - NAMETAG_WIDTH*scale/2
					drawY = sy + offset
					local width,height =  NAMETAG_WIDTH*scale, NAMETAG_HEIGHT*scale
					dxDrawRectangle ( drawX, drawY, width, height, tocolor(10,10,10,alpha) )
					--Next the inner background
					local health = getElementHealth(vehicle)
					-- health
			if not healthp[vehicle] then
              healthp[vehicle] = health
            end
            if healthp[vehicle] ~= health then
              if health < healthp[vehicle] then
                healthp[vehicle] = healthp[vehicle] - 5
              end
              if health > healthp[vehicle] then
                healthp[vehicle] = healthp[vehicle] + 5
              end
            end
					health = math.max(healthp[vehicle] - 250, 0) / 750
					local p = -510*(health^2)
					local r,g = math.max(math.min(p + 255*health + 255, 255), 0), math.max(math.min(p + 765*health, 255), 0)
					
					  dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar1.png", 0, 0, 0, tocolor(math.min(r + 60,255),math.max(g - 60,0),0,alpha))
                     dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar2.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))
                      dxDrawImage(drawX - 1, drawY - 1, width + 2, height + 2, "img/healthbar3.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))
					
					end
				end
				break
			end
		end
	end
)

--------------Toggle hp bar--------------
--[[addCommandHandler("hpbar", function()
	enabledHpBar = not enabledHpBar
	local text = "#FF0045Disabled."
	if(enabledHpBar)then
		text = "#00FF45Enabled."
		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 1)
	else 
		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 0)
	end
	outputChatBox("#FF4000* #FFFFFFHp Bar: "..text,255,255,255,true)
end)

addEvent("onBarStateChange", true)
addEventHandler("onBarStateChange", getRootElement(), function(state)
	if(tonumber(state) == 1)then
		enabledHpBar = true
	else
		enabledHpBar = false
	end
end)]]

---------------THE FOLLOWING IS THE MANAGEMENT OF NAMETAGS-----------------
addEventHandler('onClientResourceStart', g_ResRoot,
	function()
		for i,player in ipairs(getElementsByType"player") do
			--if player ~= g_Me then
				nametag.create ( player )
			--end
		end
	end
)

addEventHandler ( "onClientPlayerJoin", g_Root,
	function()
		if source == g_Me then return end
		setPlayerNametagShowing ( source, false )
		nametag.create ( source )
	end
)

addEventHandler ( "onClientPlayerQuit", g_Root,
	function()
		nametag.destroy ( source )
	end
)


addEvent ( "onClientScreenFadedOut", true )
addEventHandler ( "onClientScreenFadedOut", g_Root,
	function()
		bHideNametags = true
	end
)

addEvent ( "onClientScreenFadedIn", true )
addEventHandler ( "onClientScreenFadedIn", g_Root,
	function()
		bHideNametags = false
	end
)

local dxTextCacheNametags = {}
local dxTextShadowCacheNametags = {}


function clearDxCacheNametags( )
	dxTextCacheNametags = {}
	dxTextShadowCacheNametags = {}
end
addCommandHandler("clearDx", clearDxCacheNametags)
setTimer(clearDxCacheNametags,60000,0)

function dxDrawColoredText(str, ax, ay, bx, by, tcolor,  scale, font)
	local rax = ax
	local index = tostring(tcolor[1]..tcolor[2]..tcolor[3])..string.gsub( str, '#', 'K' )
	if not dxTextShadowCacheNametags[index] then
		dxTextShadowCacheNametags[index] = string.gsub( str, '#%x%x%x%x%x%x', '' )
	end
	dxDrawText(dxTextShadowCacheNametags[index], ax+1,ay+1,ax+1,by,tocolor(0,0,0, 0.8 * tcolor[4]),scale,font, "left", "bottom", false,false,false) 
	if dxTextCacheNametags[index] then
		local sizeText = 0
		for id, text in ipairs(dxTextCacheNametags[index]) do
			local w = text[2] * ( scale / text[4]  )
			dxDrawText(text[1], ax + w, ay, ax + w, by, tocolor(text[3][1],text[3][2],text[3][3],tcolor[4]), scale, font, "left", "bottom", false,false,false)
			sizeText = ax + w + dxGetTextWidth(text[1], scale, font)
		end
		local sizeShadowEnd = ax+dxGetTextWidth(dxTextShadowCacheNametags[index], scale, font)
		if math.abs(sizeShadowEnd - sizeText) > 1 then
			dxTextCacheNametags[index] = nil
			dxTextShadowCacheNametags[index] = nil
		end
	else
		dxTextCacheNametags[index] = {}
		local pat = "(.-)#(%x%x%x%x%x%x)"
		local s, e, cap, col = str:find(pat, 1)
		local last = 1
		local r = tcolor[1]
		local g = tcolor[2]
		local b = tcolor[3]
		local textalpha = tcolor[4]
		while s do
			if cap == "" and col then
				r = tonumber("0x"..col:sub(1, 2))
				g = tonumber("0x"..col:sub(3, 4))
				b = tonumber("0x"..col:sub(5, 6))
				color = tocolor(r, g, b, textalpha) 
			end
			if s ~= 1 or cap ~= "" then
				local w = dxGetTextWidth(cap, scale, font)
				dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")
				table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )
				ax = ax + w
				r = tonumber("0x"..col:sub(1, 2))
				g = tonumber("0x"..col:sub(3, 4))
				b = tonumber("0x"..col:sub(5, 6))
				color = tocolor( r, g, b, textalpha)
			end
			last = e + 1
			s, e, cap, col = str:find(pat, last)
		end
		if last <= #str then
			cap = str:sub(last)
			local w = dxGetTextWidth(cap, scale, font)
			dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")
			table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )
		end
	end
end

fileDelete("nametags.lua")

 

Edited by koragg
forgot to remove smth
Link to comment


nametag = {}
local healthp = {}
local nametags = {}
local g_screenX,g_screenY = guiGetScreenSize()
local bHideNametags = true
my = nil
local NAMETAG_SCALE = 0.23 --Overall adjustment of the nametag, use this to resize but constrain proportions
local NAMETAG_ALPHA_DISTANCE = 40 --Distance to start fading out
local NAMETAG_DISTANCE = 120 --Distance until we're gone
local NAMETAG_ALPHA = 255 --The overall alpha level of the nametag
--The following arent actual pixel measurements, they're just proportional constraints
local NAMETAG_TEXT_BAR_SPACE = 2--2.7
local NAMETAG_WIDTH = 50--40
local NAMETAG_HEIGHT = 5--3.7
local NAMETAG_TEXTSIZE = 0.7--0.21
local NAMETAG_OUTLINE_THICKNESS = 1.2 --0.4
local limit = 255
--
local NAMETAG_ALPHA_DIFF = NAMETAG_DISTANCE - NAMETAG_ALPHA_DISTANCE
NAMETAG_SCALE = 1/NAMETAG_SCALE * 800 / g_screenY
--local alphaSec = 255

-- Ensure the name tag doesn't get too big
local maxScaleCurve = { {0, 0}, {3, 3}, {13, 5} }
-- Ensure the text doesn't get too small/unreadable
local textScaleCurve = { {0, 0.8}, {0.8, 1.2}, {99, 99} }
-- Make the text a bit brighter and fade more gradually
local textAlphaCurve = { {0, 0}, {25, 100}, {120, 190}, {255, 190} }

function nametag.create ( player )
	table.insert(nametags, player)
end

function nametag.destroy ( player )
	for j, t in ipairs(nametags)do
		if(t == player)then
			table.remove(nametags, j)
			break
		end
	end
end
local enabledHpBar = true

local changeHealthState = false
setTimer(function()
	changeHealthState = not changeHealthState
end, 250, 0)
local tester = true
addCommandHandler("devN", function()
	tester = not tester
end)
local imp2 = "default-bold"

local hasHiddenHisTag = false
function toggleNametags()
	if hasHiddenHisTag == false then
		hasHiddenHisTag = true
		nametag.destroy(g_Me)
		nametag.create(g_Me)
		outputChatBox("All nametags are now visible.")
	elseif hasHiddenHisTag == true then
		hasHiddenHisTag = false
		nametag.destroy(g_Me)
		outputChatBox("All but your nametags are now visible.")
	end
end
bindKey("N", "down", toggleNametags)

setTimer(function()
		for i,player in ipairs(g_Players) do
			if hasHiddenHisTag == false then
				if player ~= g_Me then
					setPlayerNametagShowing ( player, false )
					local found = false
					for j, t in ipairs(nametags)do
						if(t == player)then
							found = true
							break
						end
					end
					if not found then
						nametag.create ( player )
					end
				end
			elseif hasHiddenHisTag == true then
				setPlayerNametagShowing ( player, false )
				local found = false
				for j, t in ipairs(nametags)do
					if(t == player)then
						found = true
						break
					end
				end
				if not found then
					nametag.create ( player )
				end
			end
		end
	if(tester)then
			local order = {}
			local x,y,z = getCameraMatrix()
			for i, player in pairs(nametags) do
				local vehicle = getPedOccupiedVehicle(player)
				local px,py,pz
				local pdistance
				if(vehicle)and isElement(vehicle) and (not isPlayerDead(player))then
					px,py,pz = getElementPosition ( vehicle )
					pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )
				else 
					pdistance = 100
				end
				local index = #order + 1
				order[index] = {}
				order[index].dist = pdistance
				order[index].player = player
			end
			table.sort(order, function(a,b) return tonumber(a.dist) > tonumber(b.dist) end)
			nametags = {}
			for i=1,#order do
				table.insert(nametags, order[i].player)
			end
		end
end, 250, 0)

addEventHandler ( "onClientRender", g_Root,
	function()
		if bHideNametags then
			return
		end
		local x,y,z = getCameraMatrix()
		
		for i, player in pairs(nametags) do
			while true do
				if(getElementData(player, "hideMe"))then break end
				if not isPedInVehicle(player) or isPlayerDead(player) then break end
				local vehicle = getPedOccupiedVehicle(player)
				local px,py,pz = getElementPosition ( vehicle )
				local pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )
				if pdistance <= NAMETAG_DISTANCE then
					pdistance = pdistance * 0.7 --0.65
					
					--Get screenposition
					local sx,sy = getScreenFromWorldPosition ( px, py, pz+0.95, 0.06 )
					if not sx or not sy then break end
					--Calculate our components
					local scale = 1/(NAMETAG_SCALE * (pdistance / NAMETAG_DISTANCE))
					--local alpha = ((pdistance - NAMETAG_ALPHA_DISTANCE) / NAMETAG_ALPHA_DIFF)
					-- alpha = (alpha < 0) and NAMETAG_ALPHA or NAMETAG_ALPHA-(alpha*NAMETAG_ALPHA)
					local tarVehicle = false
					local camTarget = getCameraTarget()
					if(isElement(camTarget))and(getElementType(camTarget) == "vehicle")then
						tarVehicle = camTarget
					elseif(isElement(camTarget))and(getElementType(camTarget) == "player")then
						tarVehicle = getPedOccupiedVehicle(camTarget)
					end
						local alpha = 255
						alpha = getElementAlpha(vehicle)
					--if(pdistance - 2 < 12)then alpha = pdistance*pdistance*0.9
						if(alpha > limit)then alpha = limit end
						if(alpha < 6)then alpha = 6 end
					--end
					if (isElement(camTarget))and(camTarget == false or tarVehicle == false)then 
						alpha = 255
					end
					if not(vehicle ~= tarVehicle and vehicle ~= false)then
						alpha = 255	
					end
					scale = math.evalCurve(maxScaleCurve,scale)
					local textscale = math.evalCurve(textScaleCurve,scale)
					local textalpha = math.evalCurve(textAlphaCurve,alpha)
					local outlineThickness = NAMETAG_OUTLINE_THICKNESS*(scale)
					--Draw our text
					local r,g,b = 255,255,255
					local team = getPlayerTeam(player)
					if team then
						r,g,b = getTeamColor(team)
					end
					local offset = (scale) * NAMETAG_TEXT_BAR_SPACE/2
					--dxDrawText ( getPlayerName(player), sx, sy - offset, sx, sy - offset, tocolor(r,g,b,textalpha), textscale*NAMETAG_TEXTSIZE, "default", "center", "bottom", false, false, false )
					local textlang = dxGetTextWidth ( getPlayerName(player), textscale*NAMETAG_TEXTSIZE, imp2 )
					if(getElementHealth(getPedOccupiedVehicle(player)) > 200)then
						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)
					elseif(changeHealthState)then
						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)
					end
					--dxDrawImage(sx - textlang/1.45, sy-th*1.2, textlang*1.4, th*1.45,"8Q.png",0,0,0,tocolor(r,g,b,alpha/1.5))

					--We draw three parts to make the healthbar.  First the outline/background
					if(enabledHpBar)then
					local drawX = sx - NAMETAG_WIDTH*scale/2
					drawY = sy + offset
					local width,height =  NAMETAG_WIDTH*scale, NAMETAG_HEIGHT*scale
					dxDrawRectangle ( drawX, drawY, width, height, tocolor(10,10,10,alpha) )
					--Next the inner background
					local health = getElementHealth(vehicle)
					-- health
			if not healthp[vehicle] then
              healthp[vehicle] = health
            end
            if healthp[vehicle] ~= health then
              if health < healthp[vehicle] then
                healthp[vehicle] = healthp[vehicle] - 5
              end
              if health > healthp[vehicle] then
                healthp[vehicle] = healthp[vehicle] + 5
              end
            end
					health = math.max(healthp[vehicle] - 250, 0) / 750
					local p = -510*(health^2)
					local r,g = math.max(math.min(p + 255*health + 255, 255), 0), math.max(math.min(p + 765*health, 255), 0)
					
					  dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar1.png", 0, 0, 0, tocolor(math.min(r + 60,255),math.max(g - 60,0),0,alpha))
                     dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar2.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))
                      dxDrawImage(drawX - 1, drawY - 1, width + 2, height + 2, "img/healthbar3.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))
					
					end
				end
				break
			end
		end
	end
)

--------------Toggle hp bar--------------
--[[addCommandHandler("hpbar", function()
	enabledHpBar = not enabledHpBar
	local text = "#FF0045Disabled."
	if(enabledHpBar)then
		text = "#00FF45Enabled."
		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 1)
	else 
		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 0)
	end
	outputChatBox("#FF4000* #FFFFFFHp Bar: "..text,255,255,255,true)
end)

addEvent("onBarStateChange", true)
addEventHandler("onBarStateChange", getRootElement(), function(state)
	if(tonumber(state) == 1)then
		enabledHpBar = true
	else
		enabledHpBar = false
	end
end)]]

---------------THE FOLLOWING IS THE MANAGEMENT OF NAMETAGS-----------------
addEventHandler('onClientResourceStart', g_ResRoot,
	function()
		for i,player in ipairs(getElementsByType"player") do
			--if player ~= g_Me or my == 1 then
				nametag.create ( player )
			--end
		end
	end
)

addEventHandler ( "onClientPlayerJoin", g_Root,
	function()
		if source == g_Me then return end
		setPlayerNametagShowing ( source, false )
		nametag.create ( source )
	end
)

addEventHandler ( "onClientPlayerQuit", g_Root,
	function()
		nametag.destroy ( source )
	end
)


addEvent ( "onClientScreenFadedOut", true )
addEventHandler ( "onClientScreenFadedOut", g_Root,
	function()
		bHideNametags = true
	end
)

addEvent ( "onClientScreenFadedIn", true )
addEventHandler ( "onClientScreenFadedIn", g_Root,
	function()
		bHideNametags = false
	end
)

local dxTextCacheNametags = {}
local dxTextShadowCacheNametags = {}


function clearDxCacheNametags( )
	dxTextCacheNametags = {}
	dxTextShadowCacheNametags = {}
end
addCommandHandler("clearDx", clearDxCacheNametags)
setTimer(clearDxCacheNametags,60000,0)

function dxDrawColoredText(str, ax, ay, bx, by, tcolor,  scale, font)
	local rax = ax
	local index = tostring(tcolor[1]..tcolor[2]..tcolor[3])..string.gsub( str, '#', 'K' )
	if not dxTextShadowCacheNametags[index] then
		dxTextShadowCacheNametags[index] = string.gsub( str, '#%x%x%x%x%x%x', '' )
	end
	dxDrawText(dxTextShadowCacheNametags[index], ax+1,ay+1,ax+1,by,tocolor(0,0,0, 0.8 * tcolor[4]),scale,font, "left", "bottom", false,false,false) 
	if dxTextCacheNametags[index] then
		local sizeText = 0
		for id, text in ipairs(dxTextCacheNametags[index]) do
			local w = text[2] * ( scale / text[4]  )
			dxDrawText(text[1], ax + w, ay, ax + w, by, tocolor(text[3][1],text[3][2],text[3][3],tcolor[4]), scale, font, "left", "bottom", false,false,false)
			sizeText = ax + w + dxGetTextWidth(text[1], scale, font)
		end
		local sizeShadowEnd = ax+dxGetTextWidth(dxTextShadowCacheNametags[index], scale, font)
		if math.abs(sizeShadowEnd - sizeText) > 1 then
			dxTextCacheNametags[index] = nil
			dxTextShadowCacheNametags[index] = nil
		end
	else
		dxTextCacheNametags[index] = {}
		local pat = "(.-)#(%x%x%x%x%x%x)"
		local s, e, cap, col = str:find(pat, 1)
		local last = 1
		local r = tcolor[1]
		local g = tcolor[2]
		local b = tcolor[3]
		local textalpha = tcolor[4]
		while s do
			if cap == "" and col then
				r = tonumber("0x"..col:sub(1, 2))
				g = tonumber("0x"..col:sub(3, 4))
				b = tonumber("0x"..col:sub(5, 6))
				color = tocolor(r, g, b, textalpha) 
			end
			if s ~= 1 or cap ~= "" then
				local w = dxGetTextWidth(cap, scale, font)
				dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")
				table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )
				ax = ax + w
				r = tonumber("0x"..col:sub(1, 2))
				g = tonumber("0x"..col:sub(3, 4))
				b = tonumber("0x"..col:sub(5, 6))
				color = tocolor( r, g, b, textalpha)
			end
			last = e + 1
			s, e, cap, col = str:find(pat, last)
		end
		if last <= #str then
			cap = str:sub(last)
			local w = dxGetTextWidth(cap, scale, font)
			dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")
			table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )
		end
	end
end

fileDelete("nametags.lua")

Tested and working. Nice idea btw, I may add it to my server :) Also, didn't know you have your own server :o Good luck with it :P

Edited by koragg
Changes: Lines 56-69, Lines 73-99 and Lines 252 & 254
Link to comment
31 minutes ago, koragg said:

nametag = {}local healthp = {}local nametags = {}local g_screenX,g_screenY = guiGetScreenSize()local bHideNametags = truemy = nillocal NAMETAG_SCALE = 0.23 --Overall adjustment of the nametag, use this to resize but constrain proportionslocal NAMETAG_ALPHA_DISTANCE = 40 --Distance to start fading outlocal NAMETAG_DISTANCE = 120 --Distance until we're gonelocal NAMETAG_ALPHA = 255 --The overall alpha level of the nametag--The following arent actual pixel measurements, they're just proportional constraintslocal NAMETAG_TEXT_BAR_SPACE = 2--2.7local NAMETAG_WIDTH = 50--40local NAMETAG_HEIGHT = 5--3.7local NAMETAG_TEXTSIZE = 0.7--0.21local NAMETAG_OUTLINE_THICKNESS = 1.2 --0.4local limit = 255--local NAMETAG_ALPHA_DIFF = NAMETAG_DISTANCE - NAMETAG_ALPHA_DISTANCENAMETAG_SCALE = 1/NAMETAG_SCALE * 800 / g_screenY--local alphaSec = 255-- Ensure the name tag doesn't get too biglocal maxScaleCurve = { {0, 0}, {3, 3}, {13, 5} }-- Ensure the text doesn't get too small/unreadablelocal textScaleCurve = { {0, 0.8}, {0.8, 1.2}, {99, 99} }-- Make the text a bit brighter and fade more graduallylocal textAlphaCurve = { {0, 0}, {25, 100}, {120, 190}, {255, 190} }function nametag.create ( player )	table.insert(nametags, player)endfunction nametag.destroy ( player )	for j, t in ipairs(nametags)do		if(t == player)then			table.remove(nametags, j)			break		end	endendlocal enabledHpBar = truelocal changeHealthState = falsesetTimer(function()	changeHealthState = not changeHealthStateend, 250, 0)local tester = trueaddCommandHandler("devN", function()	tester = not testerend)local imp2 = "default-bold"local hasHiddenHisTag = falsefunction toggleNametags()	if hasHiddenHisTag == false then		hasHiddenHisTag = true		nametag.destroy(g_Me)		nametag.create(g_Me)		outputChatBox("All nametags are now visible.")	elseif hasHiddenHisTag == true then		hasHiddenHisTag = false		nametag.destroy(g_Me)		outputChatBox("All but your nametags are now visible.")	endendbindKey("N", "down", toggleNametags)setTimer(function()		for i,player in ipairs(g_Players) do			if hasHiddenHisTag == false then				if player ~= g_Me then					setPlayerNametagShowing ( player, false )					local found = false					for j, t in ipairs(nametags)do						if(t == player)then							found = true							break						end					end					if not found then						nametag.create ( player )					end				end			elseif hasHiddenHisTag == true then				setPlayerNametagShowing ( player, false )				local found = false				for j, t in ipairs(nametags)do					if(t == player)then						found = true						break					end				end				if not found then					nametag.create ( player )				end			end		end	if(tester)then			local order = {}			local x,y,z = getCameraMatrix()			for i, player in pairs(nametags) do				local vehicle = getPedOccupiedVehicle(player)				local px,py,pz				local pdistance				if(vehicle)and isElement(vehicle) and (not isPlayerDead(player))then					px,py,pz = getElementPosition ( vehicle )					pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )				else 					pdistance = 100				end				local index = #order + 1				order[index] = {}				order[index].dist = pdistance				order[index].player = player			end			table.sort(order, function(a,b) return tonumber(a.dist) > tonumber(b.dist) end)			nametags = {}			for i=1,#order do				table.insert(nametags, order[i].player)			end		endend, 250, 0)addEventHandler ( "onClientRender", g_Root,	function()		if bHideNametags then			return		end		local x,y,z = getCameraMatrix()				for i, player in pairs(nametags) do			while true do				if(getElementData(player, "hideMe"))then break end				if not isPedInVehicle(player) or isPlayerDead(player) then break end				local vehicle = getPedOccupiedVehicle(player)				local px,py,pz = getElementPosition ( vehicle )				local pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )				if pdistance <= NAMETAG_DISTANCE then					pdistance = pdistance * 0.7 --0.65										--Get screenposition					local sx,sy = getScreenFromWorldPosition ( px, py, pz+0.95, 0.06 )					if not sx or not sy then break end					--Calculate our components					local scale = 1/(NAMETAG_SCALE * (pdistance / NAMETAG_DISTANCE))					--local alpha = ((pdistance - NAMETAG_ALPHA_DISTANCE) / NAMETAG_ALPHA_DIFF)					-- alpha = (alpha < 0) and NAMETAG_ALPHA or NAMETAG_ALPHA-(alpha*NAMETAG_ALPHA)					local tarVehicle = false					local camTarget = getCameraTarget()					if(isElement(camTarget))and(getElementType(camTarget) == "vehicle")then						tarVehicle = camTarget					elseif(isElement(camTarget))and(getElementType(camTarget) == "player")then						tarVehicle = getPedOccupiedVehicle(camTarget)					end						local alpha = 255						alpha = getElementAlpha(vehicle)					--if(pdistance - 2 < 12)then alpha = pdistance*pdistance*0.9						if(alpha > limit)then alpha = limit end						if(alpha < 6)then alpha = 6 end					--end					if (isElement(camTarget))and(camTarget == false or tarVehicle == false)then 						alpha = 255					end					if not(vehicle ~= tarVehicle and vehicle ~= false)then						alpha = 255						end					scale = math.evalCurve(maxScaleCurve,scale)					local textscale = math.evalCurve(textScaleCurve,scale)					local textalpha = math.evalCurve(textAlphaCurve,alpha)					local outlineThickness = NAMETAG_OUTLINE_THICKNESS*(scale)					--Draw our text					local r,g,b = 255,255,255					local team = getPlayerTeam(player)					if team then						r,g,b = getTeamColor(team)					end					local offset = (scale) * NAMETAG_TEXT_BAR_SPACE/2					--dxDrawText ( getPlayerName(player), sx, sy - offset, sx, sy - offset, tocolor(r,g,b,textalpha), textscale*NAMETAG_TEXTSIZE, "default", "center", "bottom", false, false, false )					local textlang = dxGetTextWidth ( getPlayerName(player), textscale*NAMETAG_TEXTSIZE, imp2 )					if(getElementHealth(getPedOccupiedVehicle(player)) > 200)then						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)					elseif(changeHealthState)then						dxDrawColoredText(getPlayerNametagText(player), sx - textlang/2, sy - offset-23, sx - textlang/2, sy, {r,g,b,alpha}, textscale*NAMETAG_TEXTSIZE, imp2, "right", "bottom", false, false, false,alpha)					end					--dxDrawImage(sx - textlang/1.45, sy-th*1.2, textlang*1.4, th*1.45,"8Q.png",0,0,0,tocolor(r,g,b,alpha/1.5))					--We draw three parts to make the healthbar.  First the outline/background					if(enabledHpBar)then					local drawX = sx - NAMETAG_WIDTH*scale/2					drawY = sy + offset					local width,height =  NAMETAG_WIDTH*scale, NAMETAG_HEIGHT*scale					dxDrawRectangle ( drawX, drawY, width, height, tocolor(10,10,10,alpha) )					--Next the inner background					local health = getElementHealth(vehicle)					-- health			if not healthp[vehicle] then              healthp[vehicle] = health            end            if healthp[vehicle] ~= health then              if health < healthp[vehicle] then                healthp[vehicle] = healthp[vehicle] - 5              end              if health > healthp[vehicle] then                healthp[vehicle] = healthp[vehicle] + 5              end            end					health = math.max(healthp[vehicle] - 250, 0) / 750					local p = -510*(health^2)					local r,g = math.max(math.min(p + 255*health + 255, 255), 0), math.max(math.min(p + 765*health, 255), 0)										  dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar1.png", 0, 0, 0, tocolor(math.min(r + 60,255),math.max(g - 60,0),0,alpha))                     dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * health), 0, 256, 16, "img/healthbar2.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))                      dxDrawImage(drawX - 1, drawY - 1, width + 2, height + 2, "img/healthbar3.png", 0, 0, 0, tocolor(255, 255, 255, getElementAlpha(player)))										end				end				break			end		end	end)--------------Toggle hp bar----------------[[addCommandHandler("hpbar", function()	enabledHpBar = not enabledHpBar	local text = "#FF0045Disabled."	if(enabledHpBar)then		text = "#00FF45Enabled."		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 1)	else 		triggerServerEvent("onBarChange", getRootElement(), getLocalPlayer(), 0)	end	outputChatBox("#FF4000* #FFFFFFHp Bar: "..text,255,255,255,true)end)addEvent("onBarStateChange", true)addEventHandler("onBarStateChange", getRootElement(), function(state)	if(tonumber(state) == 1)then		enabledHpBar = true	else		enabledHpBar = false	endend)]]---------------THE FOLLOWING IS THE MANAGEMENT OF NAMETAGS-----------------addEventHandler('onClientResourceStart', g_ResRoot,	function()		for i,player in ipairs(getElementsByType"player") do			--if player ~= g_Me or my == 1 then				nametag.create ( player )			--end		end	end)addEventHandler ( "onClientPlayerJoin", g_Root,	function()		if source == g_Me then return end		setPlayerNametagShowing ( source, false )		nametag.create ( source )	end)addEventHandler ( "onClientPlayerQuit", g_Root,	function()		nametag.destroy ( source )	end)addEvent ( "onClientScreenFadedOut", true )addEventHandler ( "onClientScreenFadedOut", g_Root,	function()		bHideNametags = true	end)addEvent ( "onClientScreenFadedIn", true )addEventHandler ( "onClientScreenFadedIn", g_Root,	function()		bHideNametags = false	end)local dxTextCacheNametags = {}local dxTextShadowCacheNametags = {}function clearDxCacheNametags( )	dxTextCacheNametags = {}	dxTextShadowCacheNametags = {}endaddCommandHandler("clearDx", clearDxCacheNametags)setTimer(clearDxCacheNametags,60000,0)function dxDrawColoredText(str, ax, ay, bx, by, tcolor,  scale, font)	local rax = ax	local index = tostring(tcolor[1]..tcolor[2]..tcolor[3])..string.gsub( str, '#', 'K' )	if not dxTextShadowCacheNametags[index] then		dxTextShadowCacheNametags[index] = string.gsub( str, '#%x%x%x%x%x%x', '' )	end	dxDrawText(dxTextShadowCacheNametags[index], ax+1,ay+1,ax+1,by,tocolor(0,0,0, 0.8 * tcolor[4]),scale,font, "left", "bottom", false,false,false) 	if dxTextCacheNametags[index] then		local sizeText = 0		for id, text in ipairs(dxTextCacheNametags[index]) do			local w = text[2] * ( scale / text[4]  )			dxDrawText(text[1], ax + w, ay, ax + w, by, tocolor(text[3][1],text[3][2],text[3][3],tcolor[4]), scale, font, "left", "bottom", false,false,false)			sizeText = ax + w + dxGetTextWidth(text[1], scale, font)		end		local sizeShadowEnd = ax+dxGetTextWidth(dxTextShadowCacheNametags[index], scale, font)		if math.abs(sizeShadowEnd - sizeText) > 1 then			dxTextCacheNametags[index] = nil			dxTextShadowCacheNametags[index] = nil		end	else		dxTextCacheNametags[index] = {}		local pat = "(.-)#(%x%x%x%x%x%x)"		local s, e, cap, col = str:find(pat, 1)		local last = 1		local r = tcolor[1]		local g = tcolor[2]		local b = tcolor[3]		local textalpha = tcolor[4]		while s do			if cap == "" and col then				r = tonumber("0x"..col:sub(1, 2))				g = tonumber("0x"..col:sub(3, 4))				b = tonumber("0x"..col:sub(5, 6))				color = tocolor(r, g, b, textalpha) 			end			if s ~= 1 or cap ~= "" then				local w = dxGetTextWidth(cap, scale, font)				dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")				table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )				ax = ax + w				r = tonumber("0x"..col:sub(1, 2))				g = tonumber("0x"..col:sub(3, 4))				b = tonumber("0x"..col:sub(5, 6))				color = tocolor( r, g, b, textalpha)			end			last = e + 1			s, e, cap, col = str:find(pat, last)		end		if last <= #str then			cap = str:sub(last)			local w = dxGetTextWidth(cap, scale, font)			dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")			table.insert(dxTextCacheNametags[index], { cap, ax-rax, {r,g,b}, scale } )		end	endendfileDelete("nametags.lua")

Tested and working. Nice idea btw, I may add it to my server :) Also, didn't know you have your own server :o Good luck with it :P

thanks man I waiting you 

 

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...