Jump to content

Name tag at the top of the player.


Piorun

Recommended Posts

I've got problem with my script, so i need help. I was trying to create a nice code, but this code doesn't work.

1. How to create a script - when the player join into the server his nametag shows at the top of the head for all player, but his health bar was hide (i'm thinking i must use dxDrawText function).

Please, help. I'm really noob of lua :D.

Link to comment

Yhm, okey. I'll try.

local rootElement = getRootElement()
 
function test ( )
local px, py, pz = getElementPosition ( rootElement )
local x, y, z = getScreenFromWorldPosition( px, py, pz+1 )
if x then
     example = dxDrawText( "Imie Nazwisko", x, y )
end
end
 
function nick ( )
addEventHandler("onClientRender", rootElement, test)
end
 
addEventHandler ( "onClientResourceStart", rootElement, nick )

This is my code but it doesn't work.

What i'm doing wrong?

Link to comment
local px, py, pz = getElementPosition ( rootElement )

this is totally wrong.

in your test function do

local players = getElementsByType("player")
for key,val in ipairs(players) do
local px, py, pz = getElementPosition (val)
local x, y, z = getScreenFromWorldPosition( px, py, pz+1 )
if x then
     example = dxDrawText( "Imie Nazwisko", x, y )
end
end

not tested

Link to comment

addEventHandler("onClientRender",getRootElement(),
function()
local px,py,pz,tx,ty,tz,dist
	px,py,pz = getCameraMatrix()
for k,v in ipairs(getElementsByType("player")) do
		tx,ty,tz = getElementPosition(v)
		dist = math.sqrt((px-tx)^2 + (py-ty)^2 + (pz-tz)^2)
if dist < 30.0 then
if isLineOfSightClear(px,py,pz,tx,ty,tz,true,false,false,true,false,false,getLocalPlayer()) then
local sx,sy,sz = getPedBonePosition(v,5)
local x,y = getScreenFromWorldPosition(sx,sy,sz+0.3)
if x then
dxDrawText(getPlayerName(v),x,y,x,y,tocolor(150,50,0),0.85+(15-dist)*0.02,"bankgothic")
end
end
end
end
end
)

This is random example, should work.

Link to comment
addEventHandler("onClientRender",getRootElement(),
function()
local px,py,pz,tx,ty,tz,dist
     px,py,pz = getCameraMatrix()
for k,v in ipairs(getElementsByType("player")) do
if (v~=getLocalPlayer()) then
           tx,ty,tz = getElementPosition(v)
           dist = math.sqrt((px-tx)^2 + (py-ty)^2 + (pz-tz)^2)
if dist < 30.0 then
if isLineOfSightClear(px,py,pz,tx,ty,tz,true,false,false,true,false,false,getLocalPlayer()) then
local sx,sy,sz = getPedBonePosition(v,5)
local x,y = getScreenFromWorldPosition(sx,sy,sz+0.3)
if x then
dxDrawText(getPlayerName(v),x,y,x,y,tocolor(150,50,0),0.85+(15-dist)*0.02,"bankgothic")
end
end
end
end
end
end
)

Link to comment

Use https://wiki.multitheftauto.com/wiki/DxGetTextWidth to get the text's width and draw the text half the width the text to the left of the head. Replace your dxDrawText with these 4:

local scale =  0.85+(15-dist)*0.02;
local playerName = getPlayerName( v );
local w = dxGetTextWidth( playerName, scale, "bankgothic" );
dxDrawText(playerName,x-w/2,y,x,y,tocolor(150,50,0),scale,"bankgothic")

Link to comment
  • 2 weeks later...

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...