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

fivem scripting I want to create a basic dynamic NUI that is in the center of the screen…

  1. User

    fivem scripting I want to create a basic dynamic NUI that is in the center of the screen has two buttons one says Team A one says Team B. I want it to be dynamic once one person has pressed a button nobody else can press it but for the person who pressed it will change to a button that says Leave Team

  2. Assistant

    In order to create a dynamic NUI with buttons and custom behavior, you will need two separate files: a client-side script (in JavaScript) and an HTML interface to display buttons.

    1. Create an HTML file (for example, team_selection.html) that contains the basic structure of the NUI interface:
    <!DOCTYPE html>
    <html>
    <head>
      <style>
        /* Your CSS styles go here */
      </style>
    </head>
    <body>
      <div id="team-selection">
        <button id="teamA-button">Team A</button>
        <button id="teamB-button">Team B</button>
      </div>
      <script src="team_selection.js"></script>
    </body>
    </html>
    
    1. Now, create a JavaScript file (team_selection.js) that handles button interaction:
    const teamAButton = document.getElementById("teamA-button");
    const teamBButton = document.getElementById("teamB-button");
    
    teamAButton.onclick = () => {
      teamAButton.disabled = true;
      teamBButton.disabled = true;
      teamAButton.textContent = "Leave Team";
    }
    
    teamBButton.onclick = () => {
      teamAButton.disabled = true;
      teamBButton.disabled = true;
      teamBButton.textContent = "Leave Team";
    }
    

    This script will disable both buttons when either button is clicked and change the text to "Leave Team".

    1. Now, create a client-side FiveM script (team_selection_client.lua) that displays the NUI and handles server events.
    local display = false
    local firstTime = true
    
    -- Function to show the NUI
    function setDisplay(bool)
      display = bool
      SetNuiFocus(bool, bool)
      SendNUIMessage({
        type = "display",
        status = bool
      })
    end
    
    -- Function to handle the first time a player joins the server
    AddEventHandler('playerSpawned', function(spawn)
      if firstTime then
        setDisplay(true)
        firstTime = false
      end
    end)
    
    -- Event to receive button click data from NUI
    RegisterNUICallback('team_event', function(data, cb)
      if data.team == "A" then
        -- Handle the logic for the player joining team A
      elseif data.team == "B" then
        -- Handle the logic for the player joining team B
      end
      setDisplay(false)
      cb('ok')
    end)
    
    -- Close the NUI when Escape is pressed
    Citizen.CreateThread(function()
      while true do
        Citizen.Wait(0)
        if IsControlJustPressed(0, 322) and display then
          setDisplay(false)
        end
      end
    end)
    
    1. Lastly, make sure to include and start the script in your __resource.lua file:
    resource_manifest_version "44febabe-d386-4d18-afbe-5e627f4af937"
    
    client_script "team_selection_client.lua"
    
    ui_page "team_selection.html"
    
    files {
      "team_selection.html",
      "team_selection.js",
    }
    

    Now you should have a basic NUI interface that shows up only once when the player joins the server and with team button functionalities. The NUI will close when the Escape key is pressed, and you can further customize the script to handle team logic as needed.

    Remember that the provided example might require adjustments to fit your server setup, and you may want to add more logic and features according to your needs.

  3. User

    I've already got this code how would I merge what you've done into my work

    html.lua

    script.js // Update the score (example) function updateScore(newScore) { document.getElementById("score").innerText = newScore; }

    // Toggle visibility of the UI function toggleUI() { const main = document.getElementById("main"); main.style.display = main.style.display === "flex" ? "none" : "flex"; }

    window.addEventListener("message", (event) => { if (event.data.type === "updateScore") { updateScore(event.data.value); } else if (event.data.type === "toggleUI") { toggleUI(); } });

    style.css body { margin: 0; font-family: Arial, sans-serif; display: flex; justify-content: center; text-align: center; }

    #main { display: none; align-items: center; justify-content: center; position: absolute; top: 12px; padding: 6px 12px; background-color: rgba(0, 0, 0, 0.75); border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); }

    #teamA, #score, #teamB { margin: 0 8px; color: white; font-size: 16px; }

    #score { font-size: 18px; font-weight: bold; }

  4. Assistant

    To merge the code I provided with your existing files, follow these steps:

    1. Add the buttons to the html.lua file inside the div with the id main, like this:
    <!-- Previous code -->
    <div id="main">
      <div id="teamA">Team A</div>
      <div id="score">0 - 0</div>
      <div id="teamB">Team B</div>
      <!-- Add the buttons -->
      <button id="teamA-button">Join Team A</button>
      <button id="teamB-button">Join Team B</button>
    </div>
    <!-- Rest of the code -->
    
    1. Modify script.js to include the button logic:
    // Previous functions, updateScore and toggleUI
    
    const teamAButton = document.getElementById("teamA-button");
    const teamBButton = document.getElementById("teamB-button");
    
    teamAButton.onclick = () => {
      teamAButton.disabled = true;
      teamBButton.disabled = true;
      teamAButton.textContent = "Leave Team";
    }
    
    teamBButton.onclick = () => {
      teamAButton.disabled = true;
      teamBButton.disabled = true;
      teamBButton.textContent = "Leave Team";
    }
    
    // Keep the event listener code
    window.addEventListener("message", (event) => {
      // Previous event listener code for updateScore and toggleUI
    });
    
    1. No changes are needed for style.css.

    2. Update the client-side FiveM script (team_selection_client.lua) or create a new one if necessary:

    local display = false
    local firstTime = true
    
    -- Function to show the NUI
    function setDisplay(bool)
      display = bool
      SetNuiFocus(bool, bool)
      SendNUIMessage({
        type = "toggleUI",
      })
    end
    
    -- Function to handle the first time a player joins the server
    AddEventHandler('playerSpawned', function(spawn)
      if firstTime then
        setDisplay(true)
        firstTime = false
      end
    end)
    
    -- Event to receive button click data from NUI
    RegisterNUICallback('team_event', function(data, cb)
      if data.team == "A" then
        -- Handle the logic for the player joining team A
      elseif data.team == "B" then
        -- Handle the logic for the player joining team B
      end
      setDisplay(false)
      cb('ok')
    end)
    
    -- Close the NUI when Escape is pressed
    Citizen.CreateThread(function()
      while true do
        Citizen.Wait(0)
        if IsControlJustPressed(0, 322) and display then
          setDisplay(false)
        end
      end
    end)
    

    You may have to adjust the logic in the setDisplay function in the client script to match your NUI setup.

    With these changes, your scoreboard UI will now display buttons to join Team A or Team B, and when clicked, the buttons will change to "Leave Team" and get disabled. You can further customize the script to handle team logic as needed.

    Please note that you may need to adjust file names, resource manifest, or styles if necessary to fit your server setup.

  5. User

    I want the buttons to be not attached to the scoreboard but seperate

  6. Assistant

    To keep the buttons separate from the scoreboard, you can create another div container just for the buttons. Follow these steps:

    1. Update the html.lua file to separate the buttons from the scoreboard:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <!-- Previous head content -->
    </head>
    <body>
        <div id="main">
            <div id="teamA">Team A</div>
            <div id="score">0 - 0</div>
            <div id="teamB">Team B</div>
        </div>
        <!-- New container for buttons -->
        <div id="team-selection">
          <button id="teamA-button">Join Team A</button>
          <button id="teamB-button">Join Team B</button>
        </div>
        <script src="script.js"></script>
    </body>
    </html>
    
    1. Update the style.css file to position the new "team-selection" container in the center of the screen:
    /* Previous styles */
    
    /* New styles for the team-selection container */
    #team-selection {
      display: none; /* Initially hidden */
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      z-index: 10;
    }
    
    #team-selection button {
      margin: 8px;
      font-size: 16px;
      padding: 8px 16px;
    }
    
    1. Update the script.js to show the team-selection container when displaying the UI:
    // Previous functions code: updateScore and toggleUI 
    
    // Add this function for the buttons display
    function toggleTeamSelection() {
      const teamSelection = document.getElementById("team-selection");
      teamSelection.style.display = teamSelection.style.display === "block" ? "none" : "block";
    }
    
    // Previous constant definitions: teamAButton and teamBButton
    
    // Button onclick functions
    teamAButton.onclick = () => {
      teamAButton.disabled = true;
      teamBButton.disabled = true;
      teamAButton.textContent = "Leave Team";
    }
    
    teamBButton.onclick = () => {
      teamAButton.disabled = true;
      teamBButton.disabled = true;
      teamBButton.textContent = "Leave Team";
    }
    
    // Modify the event listener code
    window.addEventListener("message", (event) => {
        // Previous event listener code for updateScore and toggleUI
        if (event.data.type === "display") {
          toggleTeamSelection();
        }
    });
    
    1. Update the client-side FiveM script to display the team-selection container:
    -- Previous code: local display, firstTime, and setDisplay function
    
    -- Update the setDisplay function
    function setDisplay(bool)
      display = bool
      SetNuiFocus(bool, bool)
      SendNUIMessage({
        type = "display",
      })
    end
    
    -- Rest of the FiveM client-side script (no changes needed)
    
    

    Now the team-selection container with the buttons will be displayed separately from the scoreboard and appear in the center of the screen. The rest of the logic and styling should work as before, and you can further customize it as needed.

    Please note that you may need to adjust file names, resource manifest, or styles to fit your server setup.

  7. User

    can the team selection div be in the center of the screen

  8. Assistant

    In the previous response, I provided a solution that places the team selection div in the center of the screen. The style.css code snippet to achieve this is as follows:

    #team-selection {
      display: none; /* Initially hidden */
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      z-index: 10;
    }
    
    #team-selection button {
      margin: 8px;
      font-size: 16px;
      padding: 8px 16px;
    }
    

    This CSS ensures that the team-selection div container is in the center of the screen. It positions the container using the top, left, and transform properties. display: none; hides the container initially, while the JavaScript code changes this property to be visible on specific events.

    Make sure to use this CSS in your style.css file and the new container code in your html.lua, so that the buttons appear centered on the screen.