Setting/Changing Client variables with variables from database

I am trying to create a territory script for qb-core.

Problems that I am struggling with:

  1. set a client’s local variable with the variable I get from Database. (managed to get the database variable into client but can’t make it to change the local one.
  2. if I change my gang with the script running, my script doesn’t get the new gang but stays in the old one until I restart the script. ( If I exec the command “test” without first running the function pg() the variable is nil as it supposed to be, but when I change the gang and rerun the function nothing changes)

P.S. any advice on where to look to find something similar and make me understand how to do it would be great as well. Really trying to learn ( 3 days struggling with this thing :stuck_out_tongue: , ooh also sorry for all those prints and for function with silly names)

My Code for client:

local QBCore = exports['qb-core']:GetCoreObject()
local PlayerData = QBCore.Functions.GetPlayerData()
local gangcontrol = nil
local PlayerGang = nil

--commands--
RegisterCommand('test', function()
	print(gangcontrol)
	print(PlayerGang)

end)

RegisterCommand("test2", function() 
	print("^1Fun Started...")
    tever()	
end)

--events--
RegisterNetEvent('event')
AddEventHandler('event', function(gang)
    print('^3Server said: ' ..gang)
	print("^4 checking if server variable is =" ..gang)
	print(PlayerData.gang.name, PlayerData.gang.name, PlayerData.gang.name)
	pg()
	local gangcontrol = gang
	print("^6 checking if local variable? =" ..gangcontrol, "and then server still.. " ..gang)
	print("^5 players gang is =" ..PlayerData.gang.name)
	if PlayerGang == gangcontrol then
		rap()
	else
		print("^7not happened because players gang is ^9" ..PlayerGang, "^7and gang controling the area is^9" ..gangcontrol, "^0 or ^9" ..gang)
	end
end)

--functions--
function tever()
        local source = GetPlayerServerId(PlayerId())
	print("^2tever function triggered..")
	TriggerServerEvent("test1", source)
end

function pg()
	PlayerGang = PlayerData.gang.name
	print("pg function said:"..PlayerData.gang.name, PlayerGang)
end

function rap()
	print("rap")
end

My Code for server:

RegisterServerEvent('test1')
AddEventHandler('test1', function(source, gang)
    exports.oxmysql:fetch("SELECT control FROM territories WHERE territory_id = ?", {"1"}, 
    function(result)
        TriggerClientEvent("event", source, ""..result[1].control )  
    end)  
end)