[HELP] Use MySQL database to store player information

Hi guys, I started creating a personal script, but I would like to store some player data in the MySQL database, but I have no idea how to use the mysql-async and GHmattimysql connectors, I tried to make the prototype below but apparently not it works.

The idea was that when the player spawn, he would fetch the information from the database and change the variable “activeplayerxp” and synchronize (send) every 10 seconds activeplaerxp with the database.

I’m programming in LUA

-------------------------------- Client -------------------------------

AddEventHandler(“playerSpawned”,function()
TriggerServerEvent(“gtav:ranksync”)
end)

Citizen.CreateThread(function()
while true do
Citizen.Wait(5000)
local currentxp = activeplayerxp
TriggerServerEvent(‘gatv:saverank’,currentxp)
end
end)

RegisterNetEvent(“gtav:loadranck”)
AddEventHandler(“gtav:rankload”,function(result)
local xp = result
activeplayerxp = xp
end)


-------------------------------- Server -------------------------------

RegisterServerEvent(“gtav:saverank”)
RegisterServerEvent(“gtav:ranksync”)

AddEventHandler(“gtav:saverank”, function(currentxp)
local player = user.identifier
local xp = currentxp
MySQL.Async.execute(“UPDATE vrp_user_xp SET currentxp=@xp WHERE identifier = @username”, {[’@username’] = player, [’@xp’] = xp})
end)

AddEventHandler(“gtav:ranksync”, function(source)
local player = user.identifier
local result = MySQL.Sync.fetchScalar(“SELECT xp FROM vrp_user_xp WHERE identifier = @username”, {[’@username’] = player})
if(result ~= nil)then
TriggerClientEvent(“gtav:loadranck”, source, result)
end
end)

Any help is welcome and I will be grateful for that!

1 Like