[Release] AI Tow Truck Remastered

It will because your vehicle goes away which auto does that. But maybe I would rather it be towed to a garage where I can get it repaired.

You need to code that in yourself, unless the creator of the script updates that function in

That’s kinda what I was hoping would come from my question lol.

is good mod man, thx u…but where he leaves the car???

looked through the client.lua and could not find anywhere that specified a drop location, after following the drivers around for a bit it seems that they just driver around endlessly.
I went ahead and added these two lines to the client.lua at line #95 to delete the towtruck and vehicle after 90 seconds of picking it up

Citizen.Wait(90000)
DeleteTowTruck(truck, driver)
1 Like

Great mod! But when i get towed and i am on the back of the truck, the truck just keeps driving haha. He never drops me off.

1 Like

Do you have to be within so far from the road for bob to put down the bottle and come get the car? I tried to use it in the airplane junkyard and bob never came to get it until I moved it closer to the road or is there a value I can change to fix this? Also love the fact that the command to activate script can be edited.

Good, scripts i think theres lots of things you can make better however its an amazing scripts,

Kind Regards

Vik

Add this under that to stop the car being removed -

DetachEntity(car, truck)

Also on a side note if you want it to repair here is the code to do that

RegisterCommand("tow", function(source, args) 														--Change "tow" value to change activation command.
    TriggerEvent("knb:tow")													
end, false)

AddEventHandler("knb:tow", function()
	player = GetPlayerPed(-1)
	playerPos = GetEntityCoords(player)
	
	local inFrontOfPlayer = GetOffsetFromEntityInWorldCoords(player, 0.0, 5.0, 0.0)
	
	local targetVeh = GetTargetVehicle(player, inFrontOfPlayer)
	
	GetTowTruck(targetVeh)
	GetTowDriver()
	
	local driverhash = GetHashKey(towTruckDriverPick.ped)
	RequestModel(driverhash)
	local truckhash = GetHashKey(towTruckModelsPick.model)
	RequestModel(truckhash)

	loadAnimDict("random@arrests")
	
	while not HasModelLoaded(driverhash) and RequestModel(driverhash) or not HasModelLoaded(truckhash) and RequestModel(truckhash) do
		RequestModel(driverhash)
		RequestModel(truckhash)
		Citizen.Wait(0)
	end
	
	if DoesEntityExist(targetVeh) then
		if DoesEntityExist(towTruck) then
			DeleteTowTruck(towTruck, towTruckDriver)
			SpawnTowTruck(playerPos.x, playerPos.y, playerPos.x, truckhash, driverhash)
		else
			SpawnTowTruck(playerPos.x, playerPos.y, playerPos.x, truckhash, driverhash)
		end
		playRadioAnim(player)
		GoToTarget(GetEntityCoords(targetVeh).x, GetEntityCoords(targetVeh).y, GetEntityCoords(targetVeh).z, towTruck, towTruckDriver, truckhash, targetVeh)
	end
	
	
end)

function SpawnTowTruck(x, y, z, truckhash, driverhash)												--Spawning Function
	local found, spawnPos, spawnHeading = GetClosestVehicleNodeWithHeading(x + math.random(-spawnRadius, spawnRadius), y + math.random(-spawnRadius, spawnRadius), z, 0, 3, 0)
	
	if found and HasModelLoaded(truckhash) and HasModelLoaded(truckhash) then
		towTruck = CreateVehicle(truckhash, spawnPos, spawnHeading, true, false)					--Truck Spawning.
		ClearAreaOfVehicles(GetEntityCoords(towTruck), 5000, false, false, false, false, false);  
		SetVehicleOnGroundProperly(towTruck)
		SetVehicleColours(towTruck, towTruckDriverPick.colour, towTruckDriverPick.colour)
		
		towTruckDriver = CreatePedInsideVehicle(towTruck, 26, driverhash, -1, true, false)			--Driver Spawning.
		
		towTruckBlip = AddBlipForEntity(towTruck)													--Blip Spawning.
		SetBlipFlashes(towTruckBlip, true)
		SetBlipColour(towTruckBlip, 29)
	end
end

function DeleteTowTruck(towTruck, towTruckDriver)
	SetEntityAsMissionEntity(towTruck, false, false)												--Truck Removal
	DeleteEntity(towTruck)
	SetEntityAsMissionEntity(towTruckDriver, false, false)											--Driver Removal
	DeleteEntity(towTruckDriver)
	RemoveBlip(towTruckBlip)																		--Blip Removal
end

function GoToTarget(x, y, z, truck, driver, truckhash, car)
	TaskVehicleDriveToCoord(driver, truck, x, y, z, 17.0, 0, truckhash, drivingStyle, 1, true)
	ShowAdvancedNotification(companyIcon, companyName, "Tow Truck Dispatched", "A tow truck has been dispatched to your location. Thanks for using ~y~" .. companyName)
	enroute = true
	while enroute == true do
		Citizen.Wait(500)
		distanceToTarget = GetDistanceBetweenCoords(GetEntityCoords(car), GetEntityCoords(truck).x, GetEntityCoords(truck).y, GetEntityCoords(truck).z, false)
		if distanceToTarget < 15 then
			TaskVehicleTempAction(driver, truck, 27, -1)
			SetVehicleDoorOpen(truck, 2, false, false)
			SetVehicleDoorOpen(truck, 3, false, false)
		elseif distanceToTarget < 20 then
			Citizen.Wait(5000)
			PickupTarget(truck, driver, car)
		end
	end
end

function PickupTarget(truck, driver, car)
	enroute = false
	AttachEntityToEntity(car, truck, 20, towTruckModelsPick.offset.x, towTruckModelsPick.offset.y, towTruckModelsPick.offset.z, 0.0, 0.0, 0.0, false, false, false, false, 20, true)
	ShowAdvancedNotification(towTruckDriverPick.icon, towTruckDriverPick.name, "Vehicle Towed" , towTruckDriverPick[1][math.random(#towTruckDriverPick[1])])
	Citizen.Wait(5000)
	SetVehicleDoorsShut(truck, false)
	StartVehicleHorn(truck, 100, 0, false)
	TaskVehicleDriveWander(driver, truck, 17.0, drivingStyle)
	Citizen.Wait(10000)
	ShowNotification("Your vehicle has been repaired, have a great day!")
	--SetVehicleEngineHealth(vehicle, 100)
	Repair()               --Calls the repair function at the base of this script
	Citizen.Wait(1000)
	
	DetachEntity(car, truck)  --Detatches the car from the truck 
	
	SetEntityAsNoLongerNeeded(car)
	SetEntityAsNoLongerNeeded(truck)
	DeleteTowTruck(truck, driver)
	SetPedAsNoLongerNeeded(driver)
	RemoveBlip(towTruckBlip)
	towTruck = nil
	towTruckDriver = nil
	targetVeh = nil
end

function GetTargetVehicle(player, dir)
	if IsPedSittingInAnyVehicle(player) then 
        towedVehicle = GetVehiclePedIsIn(player, false)
	else
		towedVehicle = GetVehicleInDirection(GetEntityCoords(player), dir)
	end
	
	if DoesEntityExist(towedVehicle) then
		return towedVehicle
	else
		ShowNotification("Failed to find a vehicle.")
	end
end

function GetTowTruck(vehicle)
	targetVehClass = GetVehicleClass(vehicle)
	if targetVehClass == 13 or targetVehClass == 8 then
		towTruckModelsPick = towTruckModels.boxtrucks
	else
		towTruckModelsPick = towTruckModels.flatbeds
	end
end

function GetTowDriver()
	towTruckDriverPick = towTruckDrivers[math.random(#towTruckDrivers)]
end

function GetVehicleInDirection(coordFrom, coordTo)
    local rayHandle = CastRayPointToPoint( coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 10, GetPlayerPed(-1), 0)
    local _, _, _, _, vehicle = GetRaycastResult(rayHandle)
    return vehicle
end

function playRadioAnim(player)
	Citizen.CreateThread(function()
		RequestAnimDict(arrests)
	    TaskPlayAnim(player, "random@arrests", "generic_radio_enter", 1.5, 2.0, -1, 50, 2.0, 0, 0, 0 )
		Citizen.Wait(6000)
		ClearPedTasks(player)
	end)
end

function ShowAdvancedNotification(icon, sender, title, text)
    SetNotificationTextEntry("STRING")
    AddTextComponentString(text)
	SetNotificationMessage(icon, icon, true, 4, sender, title, text)
    DrawNotification(false, true)
end

function loadAnimDict(dict)
	while (not HasAnimDictLoaded(dict)) do
		RequestAnimDict(dict)
		Citizen.Wait(0)
	end
end

function ShowNotification(text)
    SetNotificationTextEntry("STRING")
    AddTextComponentString(text)
    DrawNotification(false, false)
end

function Repair()

	local playerPed = GetPlayerPed(-1)
	if IsPedInAnyVehicle(playerPed, false) then
		local vehicle = GetVehiclePedIsIn(playerPed, false)
		SetVehicleEngineHealth(vehicle, 1000)
		SetVehicleEngineOn( vehicle, true, true )
		SetVehicleFixed(vehicle)
	else
		
	end
	
end



-- function GoToTargetEnd(x, y, z, truck, driver, truckhash, repairloc)
	-- TaskVehicleDriveToCoord(driver, truck, -241.56, -1305.76, 30.62, 17.0, 0, truckhash, drivingStyle, 1, true)
	-- ShowAdvancedNotification(companyIcon, companyName, "Tow truck heading to repair station ~y~" .. companyName)
	-- enrouteEnd = true
	-- while enrouteEnd == true do
		-- Citizen.Wait(500)
		-- distanceToTarget = GetDistanceBetweenCoords(GetEntityCoords(repairloc), GetEntityCoords(truck).x, GetEntityCoords(truck).y, GetEntityCoords(truck).z, false)
		-- if distanceToTarget < 15 then
			-- TaskVehicleTempAction(driver, truck, 27, -1)
			-- SetVehicleDoorOpen(truck, 2, false, false)
			-- SetVehicleDoorOpen(truck, 3, false, false)
		-- elseif distanceToTarget < 20 then
			-- Citizen.Wait(5000)
			-- DropTarget(truck, driver, repairloc)
		-- end
	-- end
-- end

-- function DropTarget(truck, driver, repairloc)
	-- enroute = false
	-- AttachEntityToEntity(repairloc, truck, 20, towTruckModelsPick.offset.x, towTruckModelsPick.offset.y, towTruckModelsPick.offset.z, 0.0, 0.0, 0.0, false, false, false, false, 20, true)
	-- ShowAdvancedNotification(towTruckDriverPick.icon, towTruckDriverPick.name, "Vehicle Dropped" , towTruckDriverPick[1][math.random(#towTruckDriverPick[1])])
	-- Citizen.Wait(5000)
	-- SetVehicleDoorsShut(truck, false)
	-- StartVehicleHorn(truck, 100, 0, false)
	-- TaskVehicleDriveWander(driver, truck, 17.0, drivingStyle)
	-- SetEntityAsNoLongerNeeded(repairloc)
	-- SetEntityAsNoLongerNeeded(truck)
	-- SetPedAsNoLongerNeeded(driver)
	-- RemoveBlip(towTruckBlip)
	-- towTruck = nil
	-- towTruckDriver = nil
	-- targetVeh = nil
	
	-- DeleteTowTruck(towTruck, towTruckDriver)
--end
1 Like

Esto no sirve

The truck does everything right, but it stays around the city.
How can I make it go to a specific area.

Could you make it drop you at the point marked on the map with the GPS?

1 Like

John, that edit is great, so I’m looking through and I see the TaskVehicleDriveWander, then the citizenwait. I’ve upped that to 40 since 10 seconds is so quick it feels like he just picked you up then despawns and your good. Then we move onto repair, another 1 second wait, then he hit detachEntity. Is there something I can put in there to have him pull over and stop before doing this, or at the least stop? As it is you just teleport off at full speed. Thanks!

1 Like

super good mod i like it alot works flawlessly, however there is a issue on my side your script works fine but i’ve put a custom recovery flatbed in and the vehicle sits in mid air at the back on the tow truck am i able to change the vehicle position so its sitting on the truck/mod i’ve got in my server. thanks

How do I set the command /tow for a specific job only, like police for example?

I got to work and modified the script a bit.

ChangeLog:

  • Added the ability to add multiple junkyards. This will be chosen at random. More can be added.
  • There is an option for the mechanic to appear after delivering the vehicle and fix the car.
  • Added car parts list, the mechanic on site randomly selects one from it and repairs it with the welding * animation.
  • Added repair driver more are also possible.

Note for the general code:
I noticed that when you call the tow truck and it then drives to the destination, it takes shortcuts from time to time despite the driving style, I can’t say why.

ai_towtruck_modified.zip (6.3 KB)

1 Like

thanks my friend, you could put to pay for the service? thanks again