This is a slight mod of: Car AI/Chasing Module [open-source][o.o.p]
Only changes I have made to it for the public to use is the ability for it to kill any current chases and also added a way to check for any nearby cars, check if its speed is over x and then have it start to chase that car.
After you kill the chase you can also restart the same chase as well by just initiating it like you would’ve before by driving fast enough within range.
The default range is 30 studs and the minimum speed you need to hit is 20 studs via magnitude.
Code is self explanatory and the only function you need to run in a loop would be new.idle()
Example usage (includes kill example and also basic usage):
repeat until game.Loaded -- wait until the game is loaded to prevent any potential issues
local car = script.Parent --< reference to the car model
local bumper = car.bmp.Attachment --< reference to the bumper ()
local seat = car:FindFirstChildOfClass("VehicleSeat") -- does not work with `Seats`
local mod = require(script.carAI)
local endchaseRemote = script.RemoteEvent -- remote for ending chase
local new = mod.new(car,bumper) -- create new ai from car
-- allow the idle script to run and chase players, it has to run in a loop
-- as the `advancedPath`/`pathToTarget` functions don't run in their own
-- loop
task.spawn(coroutine.wrap(function()
while task.wait() do
new.idle() -- car sits idle until it finds a target to chase
end
end))
-- do this anyway you want but for testing I did it as a remote
endchaseRemote.OnServerEvent:Connect(function(player)
if new.inChase == true then -- if inside of a chase
new.inChase = false -- stop chasing
new.isIdle = true -- become idle
new.ChaseTarget = nil -- remove chase target
local s: VehicleSeat? = new.seat -- get the seat of the car
if s then -- if it exists
seat.ThrottleFloat = 0
seat.SteerFloat = 0
seat.Torque = 0
end
end
end)
Preview video:
Downloads:
Did not bother uploading to Roblox due to the fact I probably won’t update this.
If there is any suggestions for performance improvements let me know. I know it uses around 1% activity when idle and 2-3% when actually running.