3D Text for all players

Hey guys, I used this pre made 3Dtext function in my client.lua to make it create an text above the players head with a command. It all worked just fine. Now is the problm that only the client can see it so I need to put it to my server.lua but those natives from the clientside does not exist on server side. Is there a way to do it easy? I am developing since 2022 in fivem lua and cant figure it out, thank you for your help!

(Sorry for my english ^ ^)

EDIT:
image

I assume the flow you are trying to describe is:
Client 1 runs command > All clients see a message above Client 1’s head
and what you are describing as happening is:
Client 1 runs command > Client 1 sees message above Client 1’s head

The DrawText native and its SetText options are all client side only natives, the server can’t draw text, it has no graphics display for the instance of the game it is running.

What you need to do is trigger a server event after Client 1 runs the command, and that server event needs to tell every client to draw the text above Client 1’s head.

Client-side

local playerTexts = {}
local distance = 20.0 -- Max distance to drawtext

function DrawTextFunction(...)
    -- do drawtext
end

CreateThread(function()
    while true do
        Wait(1)
        if #playerTexts > 0 then
            local coords = GetEntityCoords(PlayerPedId())
            for _,info in pairs(playerTexts) do
                local textCoords = info.coords
                if #(textCoords - coords) < distance then
                    DrawTextFunction(info.coords,info.text,info.color)
                end
            end
        else
            Wait(500)
        end
    end
end)

RegisterNetEvent("drawText:addNewText")
AddEventHandler("drawText:addNewText", function(info)
    table.insert(playerTexts, info)
end)


RegisterCommand("drawtext", function(source,args,raw)
    local text = args[1]
    local coords = args[2]
    local color = args[3]
    --more stuff..

    TriggerServerEvent("drawText:newText", text, coords, color)
end)

Server-side

RegisterServerEvent("drawText:newText")
AddEventHandler("drawText:newText", function(text, coords, color, --[[..more stuff]])
    local player = source
    TriggerClientEvent("drawText:addNewText", -1, {text = text, coords = coords, color = color})
end)

This code should help you. It’s a really bad example and you should secure it or use it differently, but it’s what you need.

Hey , first of all, thank you all for your help! I will try out your code thank you. I dont really undersand the „for loop“ thing. Could you may explain your client side?

Client-side:

  • A local variable playerTexts is declared as an empty table to store information about text to be drawn.
  • A local variable distance is declared and set to 20.0 as the maximum distance to draw the text.
  • DrawTextFunction is declared as a function with a variable number of arguments (...). This function will handle the actual process of drawing the text.
  • A coroutine CreateThread is created that runs indefinitely and performs the following actions:
    • Wait for 1 millisecond.
    • Check if the number of entries in the playerTexts array is greater than 0.
      • If it is, get the local player’s coordinates and loop through the entries in playerTexts using pairs.
        • Calculate the distance between the local player’s coordinates and the textCoords of the current entry.
        • If the distance is less than distance, call DrawTextFunction with the current entry’s coords, text, and color as arguments.
      • If the number of entries in playerTexts is 0, wait for 500 milliseconds.
  • The event "drawText:addNewText" is registered using RegisterNetEvent.
  • An event handler is added using AddEventHandler to "drawText:addNewText" that will receive information about text to be drawn and insert it into the playerTexts table.
  • A command "drawtext" is registered using RegisterCommand that can be triggered by the player. The first argument args[1] is taken as the text, args[2] as the coordinates, and args[3] as the color of the text to be drawn.
    • The event "drawText:newText" is triggered with the text, coords, and color as arguments.

Server-side:

  • The event "drawText:newText" is registered using RegisterServerEvent.
  • An event handler is added using AddEventHandler to "drawText:newText" that receives the text, coords, and color of the text to be drawn.
    • The player who triggered the event is determined using source.
    • The event "drawText:addNewText" is triggered on all clients (-1) with the text, coords, and color of the text to be drawn.

via openai