[HELP] Get vehicle plate to server side

I’m trying to verify the current vehicle plate with the one in the database, current script crashes my entire PC because of MySQL :slight_smile:

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

1 Like

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.

2 Likes

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 have reworked the code, console outputs not ok, I guess it’s a mistake in the if statement? I really do not know how should I go by this at all

ESX              = nil

TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

RegisterNetEvent('qbc_garage:checkstate')
AddEventHandler('qbc_garage:checkstate', function(vehicle)
	local _source = source
	xPlayer = ESX.GetPlayerFromId(_source)

	MySQL.Async.fetchAll('SELECT * FROM owned_vehicles WHERE owner = @identifier AND plate = @plate',{
		['@identifier'] = xPlayer.getIdentifier(),
		['@plate'] = vehicle.plate
	}, function(data)
		if data then
			print('ok!')
		else
			print('not ok!')
		end
	end)
end)

1 Like

haha noticed this too :laughing: but ig I’ll show how I’d do this on my server
server:

local playerVehicles = SQL:execute("SELECT columns_i_actually_need_not_* FROM vehicles WHERE citizen_id=? AND plate=?", {char.citizen_id, plate})

it should just be a 1 liner…

1 Like

Lemme try this. Also can you explain the “char.” please? :slight_smile:

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 :kissing_smiling_eyes:. 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. :slight_smile:

3 Likes

Oh, thanks man! Saved me a lot of hustle!

1 Like