fivem scripting index.html <!DOCTYPE html> <html lang="en"> <head> <meta…
- User
fivem scripting index.html
style.css
body { margin: 0; font-family: Arial, sans-serif; display: flex; justify-content: center; text-align: center; }
#main { display: flex; 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; }
script.js
// Update the score (example) function updateScore(newScore) { document.getElementById("score").innerText = newScore; }
// Usage //updateScore("2-3");
window.addEventListener("message", (event) => { if (event.data.type === "updateScore") { console.log(event.data.value) updateScore(event.data.value); } });
could you create a lua event that would trigger a javascript function that toggles the UI on and off without needing an argument
- Assistant
To create a Lua event that triggers a JavaScript function to toggle the UI on and off without needing an argument, you can follow these steps:
- Modify your
script.jsfile to add a new function namedtoggleUI.
// script.js // ... // Toggle visibility of the UI function toggleUI() { const main = document.getElementById("main"); main.style.display = main.style.display === "none" ? "flex" : "none"; } // ... window.addEventListener("message", (event) => { // ... else if (event.data.type === "toggleUI") { toggleUI(); } });- Create a new Lua file (e.g.,
main.lua) to register the client-side event. This event will be received from the server to toggle the UI.
-- main.lua RegisterNetEvent("your_event_name:toggleUI") AddEventHandler("your_event_name:toggleUI", function() SendNUIMessage({ type = "toggleUI" }) end)- Create a new Lua file (e.g.,
server.lua) to trigger the event when needed. You can trigger the event from the server-side usingTriggerClientEvent()function. This example demonstrates triggering the event when a command/toggleUIis entered.
-- server.lua RegisterCommand("toggleUI", function(source, args, rawCommand) TriggerClientEvent("your_event_name:toggleUI", source) end, false)- Add the following lines inside the
fxmanifest.luato include the newly created Lua files.
-- fxmanifest.lua client_script "main.lua" server_script "server.lua"Now, when a player types
/toggleUIin the game, it will trigger thetoggleUIevent, which in turn toggles the UI on and off in thescript.jsfile. - Modify your