Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

create me a fivem hud script

  1. User

    create me a fivem hud script

  2. Assistant

    Creating a HUD (Heads-Up Display) script for FiveM can vary in complexity depending on the features you want to include. Below is a simple example of a HUD script using Lua that displays the player's health, armor, money, and a basic radar.

    This script uses the basic FiveM client-side features. Make sure you have the necessary resources set up in your FiveM server for this to work. Create a new resource folder in your resources directory named my_custom_hud, then create a __resource.lua or fxmanifest.lua, and a client.lua file as outlined below.

    Step 1: Create fxmanifest.lua

    fx_version 'cerulean'
    game 'gta5'
    
    author 'YourName'
    description 'Simple HUD for FiveM'
    version '1.0.0'
    
    client_scripts {
        'client.lua',
    }
    

    Step 2: Create client.lua

    Citizen.CreateThread(function()
        while true do
            Citizen.Wait(0) -- This makes sure the loop runs every frame
    
            -- Get player's health, armor, and money
            local playerPed = PlayerPedId()
            local health = GetEntityHealth(playerPed) - 100 -- Health Example
            local armor = GetPedArmour(playerPed)
            local playerId = PlayerId()
            local money = GetAccountMoney("bank") -- Replace with your money method
    
            -- Draw the HUD elements
            drawHud(health, armor, money)
        end
    end)
    
    function drawHud(health, armor, money)
        -- HUD Background
        drawRectangle(0.025, 0.025, 0.2, 0.1, 0, 0, 0, 150)
    
        -- Health
        drawText(0.03, 0.03, "Health: " .. health .. "%", 0.4)
    
        -- Armor
        drawText(0.03, 0.05, "Armor: " .. armor .. "%", 0.4)
    
        -- Money
        drawText(0.03, 0.07, "Money: $" .. money, 0.4)
    end
    
    function drawRectangle(x, y, width, height, r, g, b, a)
        -- Drawing a rectangle on the screen
        local w = width * GetScreenResolution()
        local h = height * GetScreenResolution()
    
        DrawRect(x + (w / 2), y + (h / 2), w, h, r, g, b, a)
    end
    
    function drawText(x, y, text, scale)
        -- Draw text on screen
        SetTextScale(scale, scale)
        SetTextFont(0)
        SetTextProportional(1)
        SetTextColour(255, 255, 255, 255)
        SetTextEntry("STRING")
        AddTextComponentString(text)
        DrawText(x, y)
    end
    
    function GetScreenResolution()
        local screenW, screenH = GetActiveScreenResolution()
        return screenW / 1920, screenH / 1080
    end
    

    Step 3: Edit server.cfg

    Make sure to add your resource to the server.cfg file to ensure it loads when you start your server:

    start my_custom_hud
    

    Explanation of the Code

    • fxmanifest.lua: Defines the resource metadata like version, author, and the client script file.
    • client.lua: Contains the logic to retrieve player stats (health, armor, money) and draw them on the screen.
    • drawHud function: Responsible for displaying the HUD on the screen.
    • drawRectangle and drawText functions: Helper functions for rendering the background and text.

    Installation

    1. Create a folder named my_custom_hud in the resources directory of your FiveM server.
    2. Create fxmanifest.lua and client.lua files inside that folder and paste the respective code snippets.
    3. Add start my_custom_hud to your server.cfg.
    4. Start your FiveM server and test it in-game.

    Note

    This is a very basic HUD that can be extended with more features such as speed, GPS direction, etc. Make sure to customize and expand this code according to your server's needs.