I’m trying to verify the current vehicle plate with the one in the database, current script crashes my entire PC because of MySQL
server.lua
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
RegisterNetEvent('qbc_garage:checkstate')
AddEventHandler('qbc_garage:checkstate', function(vehicle)
local vehicles = {}
MySQL.Async.fetchAll('SELECT * FROM owned_vehicles WHERE owner = @identifier',{
['@identifier'] = xPlayer.getIdentifier()
}, function(data)
for _, v in pairs(data) do
vehicles = table.insert(data, {
model = v.model,
plate = v.plate
})
end
if vehicles.plate == vehicle.plate then
print("ok!")
else
print("not ok")
end
end)
end)
client.lua
function StoreVeh()
local playerPed = PlayerPedId()
if IsPedInAnyVehicle(playerPed, false) then
vehicle.model = GetEntityModel(GetVehiclePedIsIn(playerPed))
vehicle.plate = GetVehicleNumberPlateText(GetVehiclePedIsIn(playerPed))
TriggerServerEvent('qbc_garage:checkstate', vehicle)
else
print('Not in a vehicle!')
end
end
Idk why you’d do this (just set a Decor or StateBag on the player owned vehicle when you spawn it?), but your logic is wrong. You need to use an AND statement for what you’re trying to do: SQL AND, OR, NOT Operators
There’s so much wrong with the code itself, but this part is probably the worst, this makes absolutely no sense, you’re not looping the table and checking each element’s plate, you’re trying to access a null variable.
It kind of works now but I should rework the code. What I’m trying to make is a garage script which stores your vehicle based on ESX but I don’t get how to use the ESX.RegisterServerCallback and ESX.TriggerServerCallback functions so I tried to make a workaround. I’m new to this stuff
I don’t use ESX or anything and char is just the return value of my getCharacter function. SQL is my own mysql2 wrapper, so you’ll have to make your own . I was just showing an example of how I’d do it so you can try to convert it to your own
No your code seems to be fine, the problem resides in GetVehicleNumberPlateText which actually adds an empty space at the beginning/end of your plate text, so you have to remove it manually using string:sub or use the ESX function to get vehicle properties and pass veh.plate as the data for the event.