Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

I’m trying to make a fivem NUI that displays a little points score at the top of the…

  1. User

    I’m trying to make a fivem NUI that displays a little points score at the top of the screen https://cdn.discordapp.com/attachments/1052780891300184096/1095659338003128380/image.png

    do you think i’m best to use react or just html css

  2. Assistant

    For a simple points score display at the top of the screen, you can use basic HTML and CSS. There's no need to use a more complex framework like React for this as it doesn't require any advanced interactivity or dynamic content. However, if you plan on expanding the project in the future and incorporating more complex features, using React might be a good idea to keep the codebase structured and maintainable.

  3. User

    https://cdn.discordapp.com/attachments/1052780891300184096/1095659338003128380/image.png

    could you give an example of how the code would look for a scoreboard identical to the image I posted

  4. Assistant

    Here's an example of HTML and CSS code to create a scoreboard similar to the image you posted:

    HTML:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="styles.css">
        <title>Scoreboard</title>
    </head>
    <body>
        <div class="scoreboard">
            <div class="points">
                <span class="label">Points: </span>
                <span class="score">1010</span>
            </div>
        </div>
    </body>
    </html>
    

    styles.css:

    body {
        margin: 0;
        background-color: #111;
    }
    
    .scoreboard {
        position: fixed;
        top: 0;
        right: 0;
        background-color: rgba(0, 0, 0, 0.5);
        padding: 5px 15px;
        margin: 10px;
        border-radius: 5px;
        display: inline-flex;
        align-items: center;
    }
    
    .label {
        color: #ffffff;
        font-family: Arial, sans-serif;
        font-size: 14px;
    }
    
    .score {
        color: #32CD32;
        font-family: Arial, sans-serif;
        font-size: 14px;
        font-weight: bold;
        margin-left: 5px;
    }
    

    This code snippet creates a simple scoreboard similar to the image you posted. You can adjust the styling and colors to match your desired design. The score value (1010 in this case) can be updated dynamically using JavaScript or any other programming language you're using in your project.

  5. User

    what would the javascript and lua look like it needs to take a score value from the lua and update the NUI

  6. Assistant

    First, create an HTML template with JavaScript for handling events coming from the Lua script:

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="styles.css">
        <title>Scoreboard</title>
    </head>
    <body>
        <div class="scoreboard">
            <div class="points">
                <span class="label">Points: </span>
                <span class="score">0</span>
            </div>
        </div>
        <script src="app.js"></script>
    </body>
    </html>
    

    Create a JavaScript file, app.js, to receive and process messages from the Lua script:

    app.js

    (function() {
        window.addEventListener('message', function(event) {
            if (event.data.type === 'updateScore') {
                updateScore(event.data.value);
            }
        });
    
        function updateScore(score) {
            const scoreElement = document.querySelector('.score');
            scoreElement.textContent = score;
        }
    })();
    

    Now, create a Lua script to send messages to the NUI:

    scoreboard.lua

    local enableScoreboard = true
    local points = 0
    
    -- This function will send an update to the NUI with the current points value
    function updateScore()
        SendNUIMessage({
            type = "updateScore",
            value = points
        })
    end
    
    -- This function sets the visibility of the NUI scoreboard
    function setVisible(visible)
        SetNuiFocus(visible, visible)
        SendNUIMessage({
            type = "setVisible",
            value = visible
        })
    end
    
    -- Set the initial visibility of the scoreboard
    if enableScoreboard then
        setVisible(true)
        updateScore()
    end
    
    -- Here you can add any events or conditions to modify the points value
    -- and update the NUI score display
    -- Example:
    -- Citizen.CreateThread(function()
    --     while true do
    --         Citizen.Wait(1000) -- Wait 1 second
    --         points = points + 10
    --         updateScore()
    --     end
    -- end)
    

    With these changes, the Lua script can send an updateScore message with the points value to update the scoreboard displayed by the NUI. Remember to adjust the points variable in the Lua script depending on your requirements.

    Finally, create a __resource.lua or fxmanifest.lua to ensure that your HTML, CSS, JavaScript, and Lua files are properly loaded by FiveM.

    __resource.lua:

    resource_manifest_version "77731fab-63ca-442c-a67b-abc70f28dfa5"
    
    ui_page "index.html"
    
    files {
        "index.html",
        "styles.css",
        "app.js"
    }
    
    client_script "scoreboard.lua"
    

    fxmanifest.lua (if you prefer this format):

    fx_version 'cerulean'
    game 'gta5'
    
    client_script 'scoreboard.lua'
    
    ui_page 'index.html'
    files {
        'index.html',
        'styles.css',
        'app.js'
    }
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.