[Help] Vehicle spawning removes money [Solved]

I’m using this menu to give players full access and freedome for vehicle spawning:


I’m trying to figure out how to modify the menu files in order to remove a certain amount of money (the cost for “borrowing” that vehicle, different for each one) when he spawns a vehicle with this menu.
I’m sure I should edit somewhere here but I can’t get a working money native to add money:
cl_vehicles.lua (34.1 KB)

Then I suppose I should add also the cost information to the JSON file… but adding “costs” line doesn’t seem to work.
mellotrainer.json (274.2 KB)

I don’t know if it changes anything but I’m also running this “gamemode” for my server, but I removed the car dealership from it, and other features such as car stealing (I don’t really want them).


In this “gamemode” settings i have Setup.NativeMoney = true, which I think is the money system type.

What I need are the lines that allow me to pick that cost information from the .json and removed that amount from the player’s money right after the vehicle gets spawned.

Look in the fs_freemode server.lua file for this event and see if that works for you.

AddEventHandler(‘CheckMoneyForVeh’, function(vehicle, price)

Thanks for the tip, this is what I ended up with (?)

AddEventHandler('CheckMoneyForVeh', function(model, price)
	local Source = source
  TriggerEvent('es:getPlayerFromId', Source, function(user)
    local player = user.getIdentifier()
	
    if user.getMoney() >= tonumber(price) then
       user.removeMoney(tonumber(price)) and drawNotification ("~g~Payment successful.") and moneyCheck = true
       -- Created this moneyCheck to confirm the payment before spawning the vehicle.
       -- This requirement will be needed later
	else
		drawNotification("~r~Not enough money.")
	end
end
end
end
end -- those line of ends suggest me something is wrong

local function SpawnVehicle(model, x, y, z, heading, ped)
	if not lastVehicle and GetVehiclePedIsIn(ped, false) then
		lastVehicle = GetVehiclePedIsIn(ped, false)
	end

	if IsModelValid(model) and moneyCheck = true then   -- cheks also if the payment has been done
		_LoadModel( model )
		-- from here on I modified nothing.
		local veh = CreateVehicle( model, x, y, z + 1, heading, true, true )

		if featureSpawnInsideCar then
			SetPedIntoVehicle(ped, veh, -1)
		end

		if featureDeleteLastVehicle then
			_SetEntityAsNoLongerNeeded(veh)

			if (lastVehicle) then
				if(GetVehicleNumberOfPassengers(lastVehicle) ~= 0 or IsVehicleSeatFree(lastVehicle, -1) == false) then
					drawNotification("~r~Last Vehicle could not be deleted.")
				else
					SetEntityAsMissionEntity(lastVehicle, true, true)
					DeleteVehicle(lastVehicle)
				end
			end
		end

		drawNotification("~g~Vehicle spawned!")
		lastVehicle = veh
		UpdateVehicleFeatureVariables( veh )
		toggleRadio(ped)

		SetModelAsNoLongerNeeded( veh )

		return veh 
	else
		drawNotification("~r~Invalid Model!")
	end
end

Doesn’t work.

You have too many ends, you’re missing the close brackets “)” for the two events, and I’m not sure that having mulitple ‘and’ statements after the if would actually work (I code mainly in C# so maybe that does actually work in lua, I dunno) try this:

AddEventHandler('CheckMoneyForVeh', function(model, price)
	local Source = source
  TriggerEvent('es:getPlayerFromId', Source, function(user)
    local player = user.getIdentifier()
	
    if user.getMoney() >= tonumber(price) then
       user.removeMoney(tonumber(price))
	   drawNotification ("~g~Payment successful.")
	   moneyCheck = true
       -- Created this moneyCheck to confirm the payment before spawning the vehicle.
       -- This requirement will be needed later
	else
		drawNotification("~r~Not enough money.")
	end
  end)
end)

You should really have this event call your SpawnVehicle function as well if you pass the if statement instead of doing the moneyCheck = true thing, but if you can make it work like you have above calling that function elsewhere then it’s fine I suppose.

1 Like

Doesn’t work with the moneyCheck = true thing but also with the TriggerEvent and AddEventHandler nothing happens.

AddEventHandler('CheckMoneyForVeh', function(model, price)
	local Source = source
  TriggerEvent('es:getPlayerFromId', Source, function(user)
    local player = user.getIdentifier()
	
    if user.getMoney() >= 500 then
       user.removeMoney(500)		-- Changed to a constant number in order to exclude any error from .json
	   drawNotification ("~g~Payment successful.")
	   TriggerEvent('moneyCheck', 1)
	else
		drawNotification("~r~Not enough money.")
	end
  end)
end)


local function SpawnVehicle(model, x, y, z, heading, ped)
	if not lastVehicle and GetVehiclePedIsIn(ped, false) then
		lastVehicle = GetVehiclePedIsIn(ped, false)
	end

	if IsModelValid(model) 
	   AddEventHandler('moneyCheck', 1)then

		_LoadModel( model )
		local veh = CreateVehicle( model, x, y, z + 1, heading, true, true )

		if featureSpawnInsideCar then
			SetPedIntoVehicle(ped, veh, -1)
		end

		if featureDeleteLastVehicle then
			_SetEntityAsNoLongerNeeded(veh)

			if (lastVehicle) then
				if(GetVehicleNumberOfPassengers(lastVehicle) ~= 0 or IsVehicleSeatFree(lastVehicle, -1) == false) then
					drawNotification("~r~Last Vehicle could not be deleted.")
				else
					SetEntityAsMissionEntity(lastVehicle, true, true)
					DeleteVehicle(lastVehicle)
				end
			end
		end

		drawNotification("~g~Vehicle spawned!")
		lastVehicle = veh
		UpdateVehicleFeatureVariables( veh )
		toggleRadio(ped)

		SetModelAsNoLongerNeeded( veh )

		return veh 
	else
		drawNotification("~r~Invalid Model!")
	end
end

I actually don’t code in lua since a lot of time…

I think I understand what you’re trying to do. The ‘CheckMoneyForVeh’ event is actually not necessary, you’re just looking for the code within it that removes the player money. I didn’t test this because it has required resources I don’t want to install and config but hopefully it will work for you.

Try replacing your function SpawnVehicle in cl_vehicles.lua with this below

local function SpawnVehicle(model, x, y, z, heading, ped)
	TriggerServerEvent('es:getPlayerFromId', source, function(user)
		local player = user.getIdentifier()
		if user.getMoney() >= 500 then
			user.removeMoney(500) -- Changed to a constant number in order to exclude any error from .json
			drawNotification ("~g~Payment successful.")

			-- Just in case they are in a vehicle which this trainer didn't spawn.
			if not lastVehicle and GetVehiclePedIsIn(ped, false) then
				lastVehicle = GetVehiclePedIsIn(ped, false)
			end

			if IsModelValid(model) then
				_LoadModel( model )

				local veh = CreateVehicle( model, x, y, z + 1, heading, true, true )

				if featureSpawnInsideCar then
					SetPedIntoVehicle(ped, veh, -1)
				end

				if featureDeleteLastVehicle then
					_SetEntityAsNoLongerNeeded(veh)
					-- Remove the last vehicle.
					if (lastVehicle) then
						if(GetVehicleNumberOfPassengers(lastVehicle) ~= 0 or IsVehicleSeatFree(lastVehicle, -1) == false) then
							drawNotification("~r~Last Vehicle could not be deleted.")
						else
							SetEntityAsMissionEntity(lastVehicle, true, true)
							DeleteVehicle(lastVehicle)
						end
					end
				end

				drawNotification("~g~Vehicle spawned!")
				lastVehicle = veh
				UpdateVehicleFeatureVariables( veh )
				toggleRadio(ped)

				SetModelAsNoLongerNeeded( veh )

				return veh 
			else
				drawNotification("~r~Invalid Model!")
			end
		else
			drawNotification("~r~Not enough money.")
		end
	end)
end

Oh that’s way simpler and should do exactly what I meant. There is still some kind of error, because nothing happens and the console started spamming “event es:getPlayerFromId was not safe for net” since I tried to spawn a vehicle and stopped when I logged out.

I have the feeling this happens because i’m trying to use “user.removeMoney” and/or “user.getMoney” client side, I’m coding a TriggerServerEvent to handle this in another file and send the info back to the source later.

Yes, do create a separate server event to handle this instead of doing the es:getPlayerFromId from client . You’re getting that error now because it needs to run from server and instead trigger a client event back to your cl.vehicle that would populate variable saying whether it’s ok or not to spawn the vehicle.

The money removal works, but the vehicle doesn’t spawn.
sv_vehicles (server file)

RegisterServerEvent('spawn:moneyCheck')
AddEventHandler('spawn:moneyCheck', function (price)
	local Source = tonumber(source)	
	TriggerEvent('es:getPlayerFromId', Source, function(user)
		if not price then
			price = 100
		end
		if user.getMoney() >= tonumber(price) then
			user.removeMoney(tonumber(price))
			drawNotification ("~g~Payment successful.")
			RegisterClientEvent('vehPaid', tonumber(price))
		else
			drawNotification ("~r~Not enough money!")
		end
	end)
end)

cl_vehicles (client file)

local function SpawnVehicle(model, x, y, z, heading, ped)
	-- Just in case they are in a vehicle which this trainer didn't spawn.
	if not lastVehicle and GetVehiclePedIsIn(ped, false) then
		lastVehicle = GetVehiclePedIsIn(ped, false)
	end
	cost = 100
	TriggerServerEvent('spawn:moneyCheck', tonumber(cost))
end

RegisterNetEvent('vehPaid')
AddEventHandler('vehPaid', function (price)
	if IsModelValid(model) then
		_LoadModel( model )
		
		local veh = CreateVehicle( model, x, y, z + 1, heading, true, true )

		if featureSpawnInsideCar then
			SetPedIntoVehicle(ped, veh, -1)
		end

		if featureDeleteLastVehicle then
			_SetEntityAsNoLongerNeeded(veh)
			-- Remove the last vehicle.
			if (lastVehicle) then
				if(GetVehicleNumberOfPassengers(lastVehicle) ~= 0 or IsVehicleSeatFree(lastVehicle, -1) == false) then
					drawNotification("~r~Last Vehicle could not be deleted.")
				else
					SetEntityAsMissionEntity(lastVehicle, true, true)
					DeleteVehicle(lastVehicle)
				end
			end
		end

		drawNotification("~g~Vehicle spawned!")
		lastVehicle = veh
		UpdateVehicleFeatureVariables( veh )
		toggleRadio(ped)

		SetModelAsNoLongerNeeded( veh )

		return veh 
	else
		drawNotification("~r~Invalid Model!")
	end
end)

the problem with this (I think) is that I can’t get these informations (model, x, y, z, heading, ped) to the second part of the client too, the part that actually needs them. How to I migrate those infos to the second part?

If the money removal is now working for you then try these edits. Instead of sending all the variables to the server and back to the client again to be used in the event you could alternatively store them as local variables in the client script and have the SpawnVehicle function update them before triggering the server event.

sv_vehicles (server file)

RegisterServerEvent('spawn:moneyCheck')
AddEventHandler('spawn:moneyCheck', function (price, model, x, y, z, heading, ped)
	local Source = tonumber(source)	
	TriggerEvent('es:getPlayerFromId', Source, function(user)
		if not price then
			price = 100
		end
		local paid = false
		if user.getMoney() >= tonumber(price) then
			user.removeMoney(tonumber(price))
			paid = true
		else
			paid = false
		end
		TriggerClientEvent('vehPaid', source, model, x, y, z, heading, ped, paid)
	end)
end)

cl_vehicles (client file)

local function SpawnVehicle(model, x, y, z, heading, ped)
	-- Just in case they are in a vehicle which this trainer didn't spawn.
	if not lastVehicle and GetVehiclePedIsIn(ped, false) then
		lastVehicle = GetVehiclePedIsIn(ped, false)
	end
	cost = 100
	TriggerServerEvent('spawn:moneyCheck', tonumber(cost), model, x, y, z, heading, ped)
end

RegisterNetEvent('vehPaid')
AddEventHandler('vehPaid', function(model, x, y, z, heading, ped, paid)
	if not paid then
		drawNotification ("~r~Not enough money!")
	else
		drawNotification ("~g~Payment successful.")
		if IsModelValid(model) then
			_LoadModel( model )
			
			local veh = CreateVehicle( model, x, y, z + 1, heading, true, true )

			if featureSpawnInsideCar then
				SetPedIntoVehicle(ped, veh, -1)
			end

			if featureDeleteLastVehicle then
				_SetEntityAsNoLongerNeeded(veh)
				-- Remove the last vehicle.
				if (lastVehicle) then
					if(GetVehicleNumberOfPassengers(lastVehicle) ~= 0 or IsVehicleSeatFree(lastVehicle, -1) == false) then
						drawNotification("~r~Last Vehicle could not be deleted.")
					else
						SetEntityAsMissionEntity(lastVehicle, true, true)
						DeleteVehicle(lastVehicle)
					end
				end
			end

			drawNotification("~g~Vehicle spawned!")
			lastVehicle = veh
			UpdateVehicleFeatureVariables( veh )
			toggleRadio(ped)

			SetModelAsNoLongerNeeded( veh )

			return veh 
		else
			drawNotification("~r~Invalid Model!")
		end
	end
end)

Works, thank you, you are awesome