Need help with this Async Server event

This is what I have:

TriggerServerEvent("apart:lockAppart", interiors[i].name, 1)

then server I have this

RegisterServerEvent("apart:lockAppart")
AddEventHandler('apart:lockAppart', function(name, status, apart)
  local source = source
  TriggerEvent('es:getPlayerFromId', source, function(user)
    local player = user.getIdentifier()
    local name = name
    local status = status
    local locked = 1
    local unlocked = 0
    MySQL.Async.fetchAll("SELECT * FROM user_appartement WHERE name = @nom", {['@nom'] = tostring(name)}, function (result)
    if (result) then
      locked = result[1].locked
      if locked == 1 then
        MySQL.Sync.execute("UPDATE user_appartement SET locked=@status WHERE name = @nom",{['@nom'] = player, ['@status'] = unlocked})
      else
        MySQL.Sync.execute("UPDATE user_appartement SET locked=@status WHERE name = @nom",{['@nom'] = player, ['@status'] = locked})
      end
    end
  end)
end)

I am brand new to this but I am trying to get it to return the value of locked in the sql and then set it to either 0 or 1.

anyone able to help me or explain to me how the function work from client to server ?

RegisterServerEvent("apart:lockAppart")
AddEventHandler('apart:lockAppart', function(name, status, apart)
  local source = source
  TriggerEvent('es:getPlayerFromId', source, function(user)
    local player = user.getIdentifier()
    local name = name
    local status = status
    local locked = 1
    local unlocked = 0
    MySQL.Async.fetchAll("SELECT * FROM user_appartement WHERE name = @nom", {['@nom'] = tostring(name)}, function (result)
    if (result) then
      locked = result[1].locked
      if locked == 1 then
        MySQL.Sync.execute("UPDATE user_appartement SET locked=@status WHERE name = @nom",{['@nom'] = player, ['@status'] = unlocked})
      else
        MySQL.Sync.execute("UPDATE user_appartement SET locked=@status WHERE name = @nom",{['@nom'] = player, ['@status'] = locked})
      end
    end
    end)
  end)
end)
1 Like

You are selecting all fields in the table and using only one. You are wasting server’s resources.
Also:

To exploit the result of an async method you must use a callback function

https://github.com/brouznouf/fivem-mysql-async#async