# Add vehicle

## esx\_vehicleshop

ในทุก Event ของการซื้อรถจากร้านขายรถ จะต้องเพิ่มการ Trigger พิเศษเพื่อให้ *ZOBYETEAM\_GARAGE* รับรู้ว่ามีการเพิ่มรถใหม่ ดังตัวอย่าง บรรทัดที่ 16

{% code title="esx\_vehicleshop (server)" lineNumbers="true" %}

```lua
ESX.RegisterServerCallback('esx_vehicleshop:buyVehicle', function(source, cb, model, plate)
	local xPlayer = ESX.GetPlayerFromId(source)
	local modelPrice = getVehicleFromModel(model).price

	if modelPrice and xPlayer.getMoney() >= modelPrice then
		xPlayer.removeMoney(modelPrice, "Vehicle Purchase")

		MySQL.insert('INSERT INTO owned_vehicles (owner, plate, vehicle) VALUES (?, ?, ?)', {xPlayer.identifier, plate, json.encode({model = joaat(model), plate = plate})
		}, function(rowsChanged)
			xPlayer.showNotification(TranslateCap('vehicle_belongs', plate))
			ESX.OneSync.SpawnVehicle(joaat(model), Config.Zones.ShopOutside.Pos, Config.Zones.ShopOutside.Heading,{plate = plate}, function(vehicle)
				Wait(100)
				local vehicle = NetworkGetEntityFromNetworkId(vehicle)
				Wait(300)
				TaskWarpPedIntoVehicle(GetPlayerPed(source), vehicle, -1)
				exports['zobyeteam_garage']:addVehicle(source, xPlayer.identifier, plate, {model = model, plate = plate})
			end)
			cb(true)
		end)
	else
		cb(false)
	end
end)
```

{% endcode %}

สำหรับ Event ของการซื้อรถ สามารถเรียกใช้ได้ทั้งหมด 3 วิธี โดยมีค่าที่จำเป็นต้องใส่อยู่ 4 ค่า ได้แก่

playerId(ถ้า Trigger จาก Client ใส่เป็น nil ได้), identifier, plate และ props ส่วนอีก 3 ค่า ได้แก่  vehicleType, job และ stored ใส่หรือไม่ก็ได้

ดังโค้ดตัวอย่าง:

```lua
-- Server Exports Function
exports['zobyeteam_garage']:addVehicle(playerId, identifier, plate, props, vehicleType, job, stored)

-- Server Event (Trigger From Client)
TriggerServerEvent('zobyeteam_garage:addVehicle', nil, identifier, plate, props, vehicleType, job, stored)

-- Server Event (Trigger From Server)
TriggerEvent('zobyeteam_garage:addVehicle', playerId, identifier, plate, props, vehicleType, job, stored)
```

* playerId (Trigger From Client Can Use nil)
* identifier
* plate
* props
* vehicleType(optional)
* job (optional)
* stored (optional)

## และตัวอย่างสำหรับ esx\_policejob

{% code title="esx\_policejob (server)" %}

```lua
ESX.RegisterServerCallback('esx_policejob:buyJobVehicle', function(source, cb, vehicleProps, type)
	local xPlayer = ESX.GetPlayerFromId(source)
	local price = getPriceFromHash(vehicleProps.model, xPlayer.job.grade_name, type)

	-- vehicle model not found
	if price == 0 then
		print(('[^3WARNING^7] Player ^5%s^7 Attempted To Buy Invalid Vehicle - ^5%s^7!'):format(source, vehicleProps.model))
		cb(false)
	else
		if xPlayer.getMoney() >= price then
			xPlayer.removeMoney(price, "Job Vehicle Bought")

			MySQL.insert('INSERT INTO owned_vehicles (owner, vehicle, plate, type, job, `stored`) VALUES (?, ?, ?, ?, ?, ?)', { xPlayer.identifier, json.encode(vehicleProps), vehicleProps.plate, type, xPlayer.job.name, true},
			function (rowsChanged)
				exports['zobyeteam_garage']:addVehicle(source, xPlayer.identifier, vehicleProps.plate, vehicleProps, type, xPlayer.job.name, true)
				cb(true)
			end)
		else
			cb(false)
		end
	end
end)
```

{% endcode %}
