Jump to content

showplayerhealth


Fory

Recommended Posts

  • Other Languages Moderators

I have made an example for you. If you aren't able to understand the code below, I'd suggest you to study Lua and MTA: SA Scripting. There are good topics here, on the forums.

Here are some of them: (by MTA Staff BOT, on our Discord server)

Spoiler

- https://wiki.multitheftauto.com/wiki/Main_Page
- https://wiki.multitheftauto.com/wiki/Scripting_Introduction
- https://www.lua.org/manual/5.1/
- https://wiki.multitheftauto.com/wiki/Category:Tutorials
- https://forum.multitheftauto.com/topic/34453-manuals

- https://forum.multitheftauto.com/topic/64228-the-ultimate-lua-tutorial/
- https://forum.multitheftauto.com/topic/121619-lua-for-absolute-beginners
- https://forum.multitheftauto.com/topic/95654-tut-debugging/
- https://forum.multitheftauto.com/topic/114541-tut-events/
- https://forum.multitheftauto.com/topic/117472-tut-scaling-dx/

 

Client-side:

Spoiler

-- Client-side
local screen = Vector2(guiGetScreenSize()) -- Get the client's screen resolution
local barWidth, barHeight = 200, 20 -- Just setting up the health/armor bar width/height
local playerTarget = false -- A variable to set the target player

local function render()
    if playerTarget and isElement(playerTarget) then -- Check if it's a valid player, because once the player leaves the server, it won't be valid anymore
        -- Health
        local health = getElementHealth(playerTarget) -- Get the player's health
        local healthX, healthY = screen.x / 2 - barWidth / 2, screen.y - 80
        dxDrawRectangle(healthX, healthY, barWidth, barHeight, tocolor(20, 21, 22, 255), false)
        dxDrawRectangle(healthX, healthY, (health / 100) * barWidth, barHeight, tocolor(255, 0, 0, 255), false)

        -- Armor
        local armor = getPedArmor(playerTarget) -- Get the player's armor
        if armor > 0 then
            local armorX, armorY = screen.x / 2 - barWidth / 2, healthY + (barHeight + 5)
            dxDrawRectangle(armorX, armorY, barWidth, barHeight, tocolor(20, 21, 22, 255), false)
            dxDrawRectangle(armorX, armorY, (armor / 100) * barWidth, barHeight, tocolor(0, 0, 255, 255), false)
        end
    end
end

-- Add the event on client-side, and we pass `true`, because it will be triggered remotely (by the server-side)
-- It's the same name as in triggerClientEvent function (server-side)
addEvent("hp:show", true)
addEventHandler("hp:show", localPlayer, function(target)
    playerTarget = target -- target is coming from the server-side, so let's save it now, on the client-side, assigning to playerTarget variable
    
    -- Here we are removing and adding the event to render things on the screen
    -- removeEventHandler is not really necessary here, but you can do logics to get this working the way you prefer
    removeEventHandler("onClientRender", root, render)
    addEventHandler("onClientRender", root, render)
end)

 

Server-side:

Spoiler

addCommandHandler("showhp", function(player, command, ...)
    local args = {...}
    if #args == 0 then -- Just checking if there are arguments - /showhp andr0xy - "andr0xy" is an argument
        return false
    end

    local target = getPlayerFromName(args[1]) -- Here we get the player element, from the nickname
    if not target or not isElement(target) then -- Check if it's a valid player
        return false
    end

    local px, py, pz = getElementPosition(player) -- Get my position (who used the command)
    local tx, ty, tz = getElementPosition(target) -- Get the target's position
    local distance = getDistanceBetweenPoints3D(px, py, pz, tx, ty, tz) -- Calculate the distance between them
    if distance > 5 then -- If both of you are too far, then it will not be executed. 5 meters I'd consider a good distance to check
        return false
    end

    triggerClientEvent(player, "hp:show", player, target) -- If everything is OK, so let's send the info to the client-side
end)

 

 

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