[HELP] Net event to global string

Hi everyone,

I want to get a string from an sql table. I get it server side but i want it client side in a variable, i just dont know how to do it. Here’s my code:

server.lua

RegisterServerEvent('police:checkGrade')
AddEventHandler('police:checkGrade', function()
	TriggerEvent("es:getPlayerFromId", source, function(user)
		local identifier = user.getIdentifier()
		MySQL.Async.fetchAll("SELECT * FROM police WHERE identifier LIKE @identifier;", {['@identifier'] = identifier}, 
		function(result)
			if(result) then
				gradepo = result[1].rank
				print(gradepo)                                 -- print "captain" and thats right
				TriggerClientEvent('police:ReturnGrade', gradepo)
			end
		end)
	end)
end)

RegisterServerEvent('print')               -- just used to see what the client variable "grade" is 
AddEventHandler('print', function(string)
  print("-------------------------------------------------------")
  print(string)
end)

client.lua

RegisterNetEvent('police:ReturnGrade')
AddEventHandler('police:ReturnGrade', function(gradepo)
	grade = gradepo
	TriggerServerEvent('print', gradepo)
end)

(...)
-- some code
(...)

function xxxx()
(...)
    TriggerServerEvent('police:checkGrade')
	Citizen.Wait(100)
	TriggerServerEvent('print', grade)                  -- print nil :( i want it to print the real grade like "captain"

	if(grade == "Captain") then
(...)
end

its like the RegisterNetEvent(‘police:ReturnGrade’) isn’t called. Maybe i use it wrong, maybe it can’t be used to return a global variable or maybe i write it at the wrong place. Can someone fix that please?

				TriggerClientEvent('police:ReturnGrade', gradepo)

you forgot to pass a client to send it to…

if you mean that i forgot “source” in

TriggerClientEvent('police:ReturnGrade', gradepo)

like

TriggerClientEvent('police:ReturnGrade', source, gradepo)

i just remove it to test if it works but it didnt. And i forgot to re-write it when i post this topic. Cause initially the “source” argument was there and it doesnt work neither :/.

But thank you for your answer.

did you forget local source = source on top of the event handler, given how you’re working with callbacks?

1 Like

Thank you Bromine! it works now!
I didn’t know that we need to use the global variable source in local variable :x