[Release] [ESX] /transfervehicle (Simple script addon to transfer your vehicles)

This script is an addon for esx_vehicleshop and allows users to transfer their cars to each other. Had this script for about 3 - 4 months now so I decided to release it as it had no more use to me. This works with any sort of plate variation as well as custom plates, just make sure to have the correct version of esx_vehicleshop which indexes the vehicle plates in a separate column in the database (picture showing that below)
image

Credit also goes to Kuzkay for helping me with writing this at the time.

How it works?
Get a vehicle you own (it won’t work if you don’t own it), then type /transfervehicle ID plate
E.G. I want to transfer my car with the plate 45FGH495 to ID 5 -> /transfervehicle 5 45FGH495

Copy paste this script in the server.lua file from your esx_vehicleshop resource

--type in chat /transfervehicle <player-id number> "car plate"
--Example /transfervehicle 1 "ABC 123" will transfer the car with plate "ABC 123" to the player 
-- who has the id 1 (the player of course needs to be online)

RegisterCommand('transfervehicle', function(source, args)

	
	myself = source
	other = args[1]
	
	if(GetPlayerName(tonumber(args[1])))then
			
	else
			
            TriggerClientEvent('chatMessage', source, "SYSTEM", {255, 0, 0}, "Incorrect player ID!")
			return
	end
	
	
	local plate1 = args[2]
	local plate2 = args[3]
	local plate3 = args[4]
	local plate4 = args[5]
	
  
	if plate1 ~= nil then plate01 = plate1 else plate01 = "" end
	if plate2 ~= nil then plate02 = plate2 else plate02 = "" end
	if plate3 ~= nil then plate03 = plate3 else plate03 = "" end
	if plate4 ~= nil then plate04 = plate4 else plate04 = "" end
  
  
	local plate = (plate01 .. " " .. plate02 .. " " .. plate03 .. " " .. plate04)

	
	mySteamID = GetPlayerIdentifiers(source)
	mySteam = mySteamID[1]
	myID = ESX.GetPlayerFromId(source).identifier
	myName = ESX.GetPlayerFromId(source).name

	targetSteamID = GetPlayerIdentifiers(args[1])
	targetSteamName = ESX.GetPlayerFromId(args[1]).name
	targetSteam = targetSteamID[1]
	
	MySQL.Async.fetchAll(
        'SELECT * FROM owned_vehicles WHERE plate = @plate',
        {
            ['@plate'] = plate
        },
        function(result)
            if result[1] ~= nil then
                local playerName = ESX.GetPlayerFromIdentifier(result[1].owner).identifier
				local pName = ESX.GetPlayerFromIdentifier(result[1].owner).name
				CarOwner = playerName
				print("Car Transfer ", myID, CarOwner)
				if myID == CarOwner then
					print("Transfered")
					
					data = {}
						TriggerClientEvent('chatMessage', other, "^4Vehicle with the plate ^*^1" .. plate .. "^r^4was transfered to you by: ^*^2" .. myName)
			 
						MySQL.Sync.execute("UPDATE owned_vehicles SET owner=@owner WHERE plate=@plate", {['@owner'] = targetSteam, ['@plate'] = plate})
						TriggerClientEvent('chatMessage', source, "^4You have ^*^3transfered^0^4 your vehicle with the plate ^*^1" .. plate .. "\" ^r^4to ^*^2".. targetSteamName)
				else
					print("Did not transfer")
					TriggerClientEvent('chatMessage', source, "^*^1You do not own the vehicle")
				end
			else
				TriggerClientEvent('chatMessage', source, "^1^*ERROR: ^r^0This vehicle plate does not exist or the plate was incorrectly written.")
            end
		
        end
    )
	
end)

Some screenshots
image
image
image
image

Hope you enjoy :slight_smile:

27 Likes

Thanks good work my civs is going to love this


4 of 5
:star::star::star::star:

2 Likes

I don’t use ESX, But if I did, I would definitely use it! This sounds amazing! Good job :+1:

4 Likes

This is so good thanks :slight_smile:

1 Like

hey,
where i must put the code?
in server.lua i testing many things it doesn’t works.

[ERROR] [MySQL] An error happens on MySQL for query “SELECT * FROM owned_vehicles WHERE plate = @plate {@plate=84GVQ057 }”: Champ ‘plate’ inconnu dans where clause

1 Like

Who makes it works?

1 Like

Do you have the correct version of esx_vehicleshop where the plate saves in a separate field in the database?
Based on the plate you gave, it doesn’t seem like it. Also the error basically says that the field or the plate isn’t registered anywhere in the database.

3 Likes

and update:

2 Likes

You have a very old version of Vehicle shop with the old plate system. You need to upgrade to the new system. with the different plate system.

4 Likes

Hey,
Yes I’ve a old version .
Thanks you for your answer

1 Like

yes! ty D_WOOD

1 Like

Thanks you

2 Likes

You can also then put the old vehicle plates into the plates field to be able to transfer the old cars

2 Likes

Be careful when it comes to updating your esx_vehicleshop to a version that supports this snippet. Supporting versions of esx_vehicleshop also require other resources to be upgraded as well. Any garage system, es_extended, etc.

A knowledgeable person could probably use this code and convert it to support older versions of esx_vehicleshop. However, if your server is running smooth, I wouldn’t bother updating.

Don’t get me wrong, this is a great snippet and I plan to use it, I’m just talking about those running older versions.

3 Likes

This isn’t the place to ask for it. Do it in https://forum.cfx.re/c/development/scripts please

ok i will

@3Bot i need your help i cant used my esx_vehicleshop and the script is working fine and then i take the script out and esx_vehicleshop is working can we hop into a discord call or something or you join the server an see whats happening if you do see this my discord is Bruised Knees#5345 and the server is Raster Roleplay

Just read up and update your script, it is probably outdated or has some errors, make sure you set the script up properly and have the latest version of it

If I have the older version of the vehicle shop. Do I just simply need to add plate column? And replace the scipts?

Yes. You can also add the older plates to the plate field if you want those to be transferable. Additionally, if you got too many plates to convert you can add this in the server.lua of esx_eden_garage. This script basically updates the vehicle plate in the plates column every time someone stores their car, so older cars with the old plate format can be automatically registered in.

Code

ESX.RegisterServerCallback('eden_garage:stockv',function(source,cb, vehicleProps)
	local isFound = false
	local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)
	local vehicules = getPlayerVehicles(xPlayer.getIdentifier())
	local plate = vehicleProps.plate
	print(plate)

		for _,v in pairs(vehicules) do
			if(plate == plate)then
				if(vehicleProps.model == v.model) then
					local idveh = v.id
					local vehprop = json.encode(vehicleProps)
					MySQL.Sync.execute("UPDATE owned_vehicles SET vehicle =@vehprop WHERE plate=@plate",{['@vehprop'] = vehprop, ['@plate'] = plate})
					MySQL.Sync.execute("UPDATE owned_vehicles SET plate =@plate WHERE id=@id",{['@plate'] = plate , ['@id'] = v.id}) 
					isFound = true
					break

				end
				
			end		
		end
		cb(isFound)
end)