Jump to content

Custom nametag


Recommended Posts

I'm currently making a custom nametag script for my server but i have a problem with showing the players wanted level.

It shows the wanted level.. how could i fix this?

spacer.png

local GUI = {}
local label = {}
local image = {}


function showCustomNametag (sResource)
    if sResource == getThisResource() then
        local players = getElementsByType ("player")
        for i, player in ipairs (players) do
            GUI.i = dgsCreate3DInterface (-2631.4, 609, 13.45, 1, 0.5, 200, 100, tocolor(255,255,255,255), 0, 1, 0)
            if localPlayer == player then
                dgsSetVisible (GUI.i, false)
            end
            dgsSetProperty (GUI.i, "maxDistance", 10)
            dgsSetProperty (GUI.i, "fadeDistance", 7)
            dgs3DInterfaceAttachToElement (GUI.i, player, 0, 0, 1)
            local bg = dgsCreateImage (0, 0, 1, 0.2, nil, true, GUI.i, tocolor(0,0,0,255))
            label.i = dgsCreateLabel (0, 0, 200, 15, getPlayerName (player), false, GUI.i)
            table.insert (label, label.i)
            dgsSetProperty (label.i , "textColor", tocolor(255,255,255,255))
            dgsSetProperty (label.i , "alignment", {"center", "top"})
            dgsSetProperty (label.i , "font", textH1)
            if getPlayerWantedLevel (player) > 0 then
                image.i = dgsCreateImage (0, 20, 200, 20, "assets/images/wantedLevel/"..getPlayerWantedLevel (player)..".png", false, GUI.i)
                table.insert (image, image.i)
            end
            setElementData (player, "playerNametagID", i)
        end
    end
end
addEventHandler ("onClientResourceStart", root, showCustomNametag)

function updateNick(name)
    local ID = getElementData (localPlayer, "playerNametagID")
    dgsSetText (label[ID], name)
end
addEvent ("SAnametag:updateNick", true)
addEventHandler ("SAnametag:updateNick", localPlayer, updateNick)

 

Link to comment

Try change

 

if getPlayerWantedLevel (player) > 0 then
  	image.i = dgsCreateImage (0, 20, 200, 20, "assets/images/wantedLevel/"..getPlayerWantedLevel (player)..".png", false, GUI.i)
	table.insert (image, image.i)
end

 

into
 

if (getPlayerWantedLevel (player) > 0) and (player ~= localPlayer) then
  	image.i = dgsCreateImage (0, 20, 200, 20, "assets/images/wantedLevel/"..getPlayerWantedLevel (player)..".png", false, GUI.i)
	table.insert (image, image.i)
end

 

  • Like 1
Link to comment

Hello Potato_Tomato42,

1)

you seem to have trouble understanding the Lua syntax. Here are some (but not all) faulty lines I am referring to:

            GUI.i = dgsCreate3DInterface (-2631.4, 609, 13.45, 1, 0.5, 200, 100, tocolor(255,255,255,255), 0, 1, 0)
...
            label.i = dgsCreateLabel (0, 0, 200, 15, getPlayerName (player), false, GUI.i)
...
                image.i = dgsCreateImage (0, 20, 200, 20, "assets/images/wantedLevel/"..getPlayerWantedLevel (player)..".png", false, GUI.i)

I think you want to save UI element handles for each player on your server and store them inside the table. But did you know that the expression "obj.key" is looking-up into "obj" by specifier constant string "i" and not the value of the i variable? I think you want to change the code to...

            GUI[i] = dgsCreate3DInterface (-2631.4, 609, 13.45, 1, 0.5, 200, 100, tocolor(255,255,255,255), 0, 1, 0)

... and all faulty lines based on the same syntax adjustment. The expression obj[key] does lookup into obj by key variable key.

2)

also you are not handling the case when somebody new is joining into your server. This person does also require a nametag but you are just assigning nametags to the players that are joined at the point of resource start, ignoring the case when new players arrive. Please use the onClientPlayerJoin event to assign a new nametag.

Hope this helps! ?

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