So I need help trying to make my AI plane fly to the target.
Script: (server)
local function getNearestTarget()
local nearestTarget = nil
local minDistance = math.huge
for i,v in ipairs(PlayerPlanes:GetChildren()) do
if v:IsA("Model") and v.PrimaryPart ~= nil and v ~= plane then
local distance = (v.PrimaryPart.Position - Base.Position).Magnitude
if distance < minDistance then
minDistance = distance
nearestTarget = v.PrimaryPart
end
end
end
return nearestTarget, minDistance
end
local function raycastTarget(target)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {plane}
local origin = FlyPart.Position
local direction = FlyPart.CFrame.LookVector
local rayDistance = 1000
local result = workspace:Raycast(origin, direction * rayDistance, raycastParams)
if result ~= nil then
local instance = result.Instance
local model = instance:FindFirstAncestorWhichIsA("Model")
print(instance, model)
if model == target then
return true
end
end
return false
end
RunService.Heartbeat:Connect(function(delta)
if isOn.Value == true then
FlyPart.AssemblyLinearVelocity = FlyPart.CFrame.LookVector * speed.Value
local target, distance = getNearestTarget()
if target then
local canSeeDirect = raycastTarget(target)
print(canSeeDirect)
if target.Position.X <= FlyPart.Position.X and canSeeDirect == false then
VehicleSeat.Steer = 1
elseif target.Position.X >= FlyPart.Position.X and canSeeDirect == false then
VehicleSeat.Steer = -1
else
VehicleSeat.Steer = 0
end
if target.Position.Y <= FlyPart.Position.Y then
VehicleSeat.Throttle = -1
elseif target.Position.Y >= FlyPart.Position.Y then
VehicleSeat.Throttle = 1
else
VehicleSeat.Throttle = 0
end
end
end
end)
Seems like it doesn’t work.
Any help please