like the title says.
Code sample:
local TargetModel = new.ChaseTarget or nil
-- here we resolve the controller for the target
local resolveTController = function(TModel: Model)
return require(TModel:WaitForChild("Scripts"):WaitForChild("Controller"))
end
-- because we are already targeting an player seat we will instead now just skip the below loop and keep chasing the same target
-- until it is set to nil
if TargetModel ~= nil then
if new.TController == nil then
new.TController = resolveTController(TargetModel)
end
task.spawn(coroutine.wrap(function()
local seat = TargetModel:WaitForChild("DriverSeat")
while task.wait(1/15) do
new.advancedPath(seat)
end
end))
end
if new.isIdle == true and new.inChase == false and TargetModel == nil then
for _, instance: Model in ipairs(nearbyInstances) do
if instance ~= nil and instance:WaitForChild("Inputs") then
-- since we know that the car should be there completely we will then assume the controller is there too
-- if it isnt then we will skip to the next car
new.TController = resolveTController(instance)
local Tinputs = instance:WaitForChild("Inputs")
local Throttle = Tinputs:GetAttribute("throttleInput")
if Throttle == nil or Tinputs == nil then
continue
end
--if Throttle ~= 0 and Throttle ~= nil and Tinputs ~= nil then
-- since we set both we might aswell check just incase
if new.TController == nil then
continue
end
local speed = new.TController:getChassisForwardSpeed()
-- since the returned speed is nil or is not a number type
if speed == nil or type(speed) ~= "number" then
continue -- skip it since it isnt what we want
end
if speed == 0 or speed > -1 and speed < 1 then
continue
end
-- if the speed is more than 20 mph reversing or less than -20 mph and is not still reversing then continue
--if speed > 0 and speed > 20 or speed < 0 and speed < -20 then
new.isIdle = false
new.inChase = true
new.ChaseTarget = instance -- sets the target model
task.spawn(coroutine.wrap(function()
local seat = instance:WaitForChild("DriverSeat")
while task.wait(1/15) do
new.advancedPath(seat)
end
end))
--[[else
continue
end]]
--[[else
continue
end]]
else -- if it is nil then skip
print("instnace is nil or does not have an Inputs configuration.")
continue
end
end -- end of for
-- gets ran once when the ai is started
elseif new.isIdle == true or new.inChase == false or seat == nil or new.ChaseTarget == nil then
new.inChase = false
new.ChaseTarget = nil
new.isIdle = true
new.TController = nil
new.pathfindingCompleted:Fire(true)
end