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


#1

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)

#2

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.


#3

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


#4

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


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

#6

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.


#7

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.


#8

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.


#9

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


#10

Of course, did not see that :stuck_out_tongue:


#11

It is not working :frowning:


#12

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)

#13

Trigger client event still doesn’t have the -1


#14

Thanks a lot, this fixed it :slight_smile:


#15

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


#16

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.


#17

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


#18

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.


#19

Could you possibly give me an example please?


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

Then on server, to trigger it, just do

TriggerClientEvent('UpdateCooldown', -1, cooldown)