The ai will just keep spinning and seems to just ignore my if statement. I hard coded it so it should work how I want it too. Do excuse me if I am doing some noob mistake it is 3:30am and I am not thinking the straightest.
Keep in mind the idle is ran in a loop. This is intentional as the advancedPath
would be ran in a loop otherwise regardless of the way its invoked.
Main script:
local mod = require(script.carAI)
local new = mod.new(car,bumper) -- fill in with your own
task.spawn(coroutine.wrap(function()
while task.wait() do
new.idle() -- car sits idle until it finds a target to chase
end
end))
Below I will provide code output and also the code block with some additional context.
Code snippet:
local carAI = {
isIdle = true, -- if it is idle
Chasetarget = nil, -- target to chase
inChase = false -- if it is already in a chase
}
function carAI.new(car : Model, bumper : Attachment) -- given function for more context
local new = setmetatable({}, carAI) -- create a clone of carAI to new
function new.idle()
local originPosition = new.seat.CFrame.Position
local radius = 30 -- STUDS DISTANCE
local function findNearbyInstances(position, radius)
local parts = workspace:GetChildren()
local nearbyInstances = {}
if #parts > 0 then
for _, part in ipairs(parts) do
if part:IsA("Model") and #part:GetChildren() > 0 and part:IsDescendantOf(script.Parent) == false then
if part:FindFirstChildOfClass("VehicleSeat") then
local distance = (part:FindFirstChildOfClass("VehicleSeat").CFrame.Position - position).magnitude
if distance <= radius then
table.insert(nearbyInstances, part)
end
end
end
end
end
return nearbyInstances
end
local nearbyInstances = findNearbyInstances(originPosition, radius)
local seat = new.ChaseTarget or nil
if new.isIdle == true and new.inChase == false and new.ChaseTarget == nil and seat == nil then
for _, instance: Model in ipairs(nearbyInstances) do
if instance ~= nil then
if instance:FindFirstChildOfClass("VehicleSeat") then
seat = instance:FindFirstChildOfClass("VehicleSeat")
if seat.Throttle == 1 and seat.CFrame.Position.Magnitude > 20 then
new.isIdle = false
new.inChase = true
new.ChaseTarget = seat
print(seat, new.ChaseTarget)
new.advancedPath(seat)
print(seat.CFrame.Position.Magnitude)
end
else
continue
end
else
continue
end
end
elseif new.isIdle == false and new.inChase == true and new.Chasetarget ~= nil then
new.isIdle = false
new.inChase = true
print(seat, new.ChaseTarget)
new.advancedPath(new.Chasetarget)
else
new.inChase = false
new.ChaseTarget = nil
new.isIdle = true
seat = nil
end
end
end -- end of car.new()
output:
03:23:15.685 DriveSeat DriveSeat - Server - carAI:91
03:23:15.690 45.43716049194336 - Server - carAI:94
03:23:15.700 DriveSeat DriveSeat - Server - carAI:91
03:23:15.706 26.08164405822754 - Server - carAI:94
03:23:15.706 DriveSeat DriveSeat - Server - carAI:91
03:23:15.713 44.4057731628418 - Server - carAI:94
03:23:15.723 DriveSeat DriveSeat - Server - carAI:91
03:23:15.732 26.346704483032227 - Server - carAI:94
03:23:15.733 DriveSeat DriveSeat - Server - carAI:91
03:23:15.739 43.60491943359375 - Server - carAI:94
03:23:15.750 DriveSeat DriveSeat - Server - carAI:91
03:23:15.756 26.920915603637695 - Server - carAI:94
03:23:15.756 DriveSeat DriveSeat - Server - carAI:91
03:23:15.764 42.57114791870117 - Server - carAI:94
03:23:15.775 DriveSeat DriveSeat - Server - carAI:91
03:23:15.783 27.450536727905273 - Server - carAI:94
03:23:15.783 DriveSeat DriveSeat - Server - carAI:91
03:23:15.789 41.1787223815918 - Server - carAI:94
03:23:15.800 DriveSeat DriveSeat - Server - carAI:91
03:23:15.806 28.356489181518555 - Server - carAI:94
03:23:15.806 DriveSeat DriveSeat - Server - carAI:91
03:23:15.815 40.64395523071289 - Server - carAI:94
03:23:15.824 DriveSeat DriveSeat - Server - carAI:91
03:23:15.832 29.09392738342285 - Server - carAI:94
03:23:15.832 DriveSeat DriveSeat - Server - carAI:91
03:23:15.838 39.42951965332031 - Server - carAI:94
03:23:15.851 DriveSeat DriveSeat - Server - carAI:91
03:23:15.856 30.00444793701172 - Server - carAI:94
03:23:15.856 DriveSeat DriveSeat - Server - carAI:91
03:23:15.865 38.29570007324219 - Server - carAI:94
03:23:15.875 DriveSeat DriveSeat - Server - carAI:91
03:23:15.881 29.67603302001953 - Server - carAI:94
03:23:15.881 DriveSeat DriveSeat - Server - carAI:91
03:23:15.889 37.953453063964844 - Server - carAI:94
03:23:15.900 DriveSeat DriveSeat - Server - carAI:91
03:23:15.906 29.241666793823242 - Server - carAI:94
03:23:15.906 DriveSeat DriveSeat - Server - carAI:91
03:23:15.917 37.065162658691406 - Server - carAI:94
03:23:15.934 DriveSeat DriveSeat - Server - carAI:91
03:23:16.655 26.76128387451172 - Server - carAI:94