Jump to content

Name tag at the top of the player.


Piorun

Recommended Posts

Posted

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.

Posted

Basically you will have to do something like this on every frame

1)getCameraMatrix

2) for loop through players

2.1)getElementPosition

2.2)isLineOfSightClear

3.1)getPedBonePosition

3.2)getScreenFromWorldPosition

3.3)dxDrawText

Posted

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?

Posted
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

Posted

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.

Posted

The_Ex thanks. It works really nice :).

I've got one more question. How to hide this text for local player, but shows for all players?

Posted
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
)

Posted

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")

Posted
Or just set two more parameters, alignX and alignY, to "center" in dxDrawText.

This will not give him the results that he wants.. He'll still have to use dxGetTextWidth.

  • 2 weeks later...
Posted
Or just set two more parameters, alignX and alignY, to "center" in dxDrawText.

This will not give him the results that he wants.. He'll still have to use dxGetTextWidth.

If you turn clipping off, then set identical left & right, and top & bottom parameters, you can use alignment easily.

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