[Help] DrawText server-side variable client-side?

Hello, is it possible to use drawtext to put a server-side variable client-side. e.g in my server.lua I have cooldown = 20
Can I use this or not? As it is not working for me.

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		DrawText2("Priority Cooldown: ~r~".. cooldown)
	end
end)

The only way you would be able to do this, is have the variable on both server and client. When the server changes the variable, send a client event to all clients and update their variable, too.

Then just draw that client variable in the draw text.

1 Like

Do you have an example? I tried but miserably failed.

What’s the code you have for your cooldown on the server side?

RegisterNetEvent("cooldownt")
AddEventHandler("cooldownt", function()
	cooldown = cooldown + 20
    while cooldown ~= 0 do
        Citizen.Wait(1000)
        cooldown = cooldown - 1
    end
end)

Client:

RegisterCommand("cd", function(cooldown)
    TriggerServerEvent("cooldownt")
end, false)

But with the DrawText it does not work serverside so I am not sure how to make it so that the cooldown goes down for everyone server-side.

So… on your client, also add the local cooldown = 0 to the top to initialize the variable. Then, that loop you posted above that draws the cooldown, use that to draw the variable you just made.

Add a new event handler on your clients, which will take a parameter and then set cooldown to whatever parameter is padded. Then in the loop you just posted, add a TriggerClientEvent... that will pass cooldown to the clients.

Or, you could just have the timer on the client side and trigger it to go from the server… instead of sending each value every second.

As in:
client:

local cooldown = 0

RegisterNetEvent("cooldownt")
AddEventHandler("cooldownt", function()
	cooldown = cooldown + 20
    while cooldown ~= 0 do
        Citizen.Wait(1000)
        cooldown = cooldown - 1
    end
end)

server:

RegisterCommand("cd", function()
    TriggerServerEvent("cooldownt")
end, false)

As that does not seem to be working.

TriggerClientEvent, not server event.
TriggerClientEvent("cooldownt", -1)

Of course, did not see that :stuck_out_tongue:

It is not working :frowning:

client:

local cooldown = 0

RegisterNetEvent("cooldownt")
AddEventHandler("cooldownt", function()
	cooldown = cooldown + 20
    while cooldown ~= 0 do
        Citizen.Wait(1000)
        cooldown = cooldown - 1
    end
end)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		DrawText2("Priority Cooldown: ~r~".. cooldown)
	end
end)

	function DrawText2(text)
        SetTextFont(0)
        SetTextProportional(1)
        SetTextScale(0.0, 0.50)
        SetTextDropshadow(1, 0, 0, 0, 255)
        SetTextEdge(1, 0, 0, 0, 255)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
        AddTextComponentString(text)
        DrawText(0.75, 0.15)
    end

server:

RegisterCommand("cd", function()
    TriggerClientEvent("cooldownt")
end, false)

Trigger client event still doesn’t have the -1

1 Like

Thanks a lot, this fixed it :slight_smile:

Just wondering, is there a way to make it so if someone joins it does not reset to 0?

Hm… didn’t think about that. In that case, you should probably do the countdown on the server and then send the value to the clients.

God I dont even know where to start with that, I tried many times and failed lol.

Put the while cooldown ~= 0 do loop on the server and every time it loops, TriggerClientEvent with cooldown. On clients, have an event handler that updates the variable.

Could you possibly give me an example please?

RegisterNetEvent('UpdateCooldown')
AddEventHandler('UpdateCooldown', function(newCooldown)
    cooldown = newCooldown
end)

Then on server, to trigger it, just do

TriggerClientEvent('UpdateCooldown', -1, cooldown)