Hi, I made a NPC Vehicle system for my AI Police car’s, There are suppose to pop tires when touched and the vehicle is suppose to delete once it reaches the player.
The Code:
script.Parent.Body.Primary.Touched:Connect(function(hit)
if hit.Parent == script.Value.Value then
script.car.Value:Destroy()
end
end)
script.Parent.Body.Pushbar.Touched:Connect(function(hit)
if hit.Name == "FL" or hit.Name == "FR" or hit.Name == "RR" or hit.Name == "RL" then
if hit.Parent:FindFirstChild("NPC") then
print("NPC, cant attack")
findPath(script.Value.Value)
else
print("Wheel attacked!")
hit.Anchored = true
hit.Transparency = 0.4
findPath(script.Value.Value)
end
end
end)
Full AI Code:
local myHuman = script.Parent:WaitForChild("police_car")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local head = script.Parent.Body:WaitForChild("Color")
local lowerTorso = script.Parent:WaitForChild("Torso")
-- Pursuit AI
--Fix Network Issues
for i,v in ipairs(script.Parent:GetDescendants()) do
if v:IsA("BasePart") and v:IsA("Part") and v:CanSetNetworkOwnership() then
v:SetNetworkOwner(nil)
end
end
script.Parent.Body.Primary.Touched:Connect(function(hit)
if hit.Parent == script.Value.Value then
script.car.Value:Destroy()
end
end)
script.Parent.Body.Pushbar.Touched:Connect(function(hit)
if hit.Name == "FL" or hit.Name == "FR" or hit.Name == "RR" or hit.Name == "RL" then
if hit.Parent:FindFirstChild("NPC") then
print("NPC, cant attack")
findPath(script.Value.Value)
else
print("Wheel attacked!")
hit.Anchored = true
hit.Transparency = 0.4
findPath(script.Value.Value)
end
end
end)
function walkRandomly()
local xRand = math.random(-50,50)
local zRand = math.random(-50,50)
local goal = myRoot.Position + Vector3.new(xRand,0,zRand)
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position, goal)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(waypoints) do
myHuman:MoveTo(waypoint.Position)
local timeOut = myHuman.MoveToFinished:Wait()
if not timeOut then
walkRandomly()
end
end
else
wait()
walkRandomly()
end
end
function findPath(target)
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position,target.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(waypoints) do
-- local part = Instance.new("Part")
-- part.Shape = "Ball"
-- part.Material = "Neon"
-- part.Size = Vector3.new(0.6, 0.6, 0.6)
-- part.Position = waypoint.Position
-- part.Anchored = true
-- part.CanCollide = false
-- part.Parent = game.Workspace
myHuman:MoveTo(waypoint.Position)
local timeOut = myHuman.MoveToFinished:Wait()
if not timeOut then
findPath(target)
break
end
if checkSight(target) then
repeat
myHuman:MoveTo(target.Position)
wait(0.1)
if target == nil then
break
elseif target.Parent == nil then
break
end
until checkSight(target) == false or myHuman.Health < 1 or target.Parent.Humanoid.Health < 1
break
end
if (myRoot.Position - waypoints[1].Position).magnitude > 20 then
findPath(target)
break
end
end
end
end
function checkSight(target)
local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 40)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
if hit then
if hit:IsDescendantOf(target.Parent) and math.abs(hit.Position.Y - myRoot.Position.Y) < 3 then
return true
end
end
return false
end
function findTarget()
local dist = 100000
local target = nil
local potentialTargets = {script.Parent.Pursuit.Value.Value}
local seeTargets = {}
for i,v in ipairs(workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("PoliceCarTarget") --or v:FindFirstChild("HumanoidRootPart")
if human and torso and v.Name ~= script.Parent.Name then
if (myRoot.Position - torso.Parent.UpperTorso.Position).magnitude < dist and human.Health > 0 then
table.insert(potentialTargets,torso.Parent.UpperTorso)
end
end
end
if #potentialTargets > 0 then
for i,v in ipairs(potentialTargets) do
if checkSight(v) then
table.insert(seeTargets, v)
elseif #seeTargets == 0 and (myRoot.Position - v.Position).magnitude < dist then
target = v
dist = (myRoot.Position - v.Position).magnitude
end
end
end
if #seeTargets > 0 then
dist = 100000
for i,v in ipairs(seeTargets) do
if (myRoot.Position - v.Position).magnitude < dist then
target = v
dist = (myRoot.Position - v.Position).magnitude
end
end
end
if target then
if math.random(20) == 1 then
end
end
return target
end
function died()
wait(10)
game:GetService("Debris"):AddItem(script.Parent,0.1)
end
myHuman.Died:Connect(died)
lowerTorso.Touched:Connect(function(obj)
if not obj.Parent:FindFirstChild("Humanoid") then
myHuman.Jump = true
end
end)
function main()
local target = findTarget()
if target then
script.Parent.Body.ELS.on.Value = 3
script.Parent.Body.ELS.Siren.Wail:Play()
myHuman.WalkSpeed = 40 -- weehoo
findPath(target)
else
script.Parent.Body.ELS.on.Value = 0
script.Parent.Body.ELS.Siren.Wail:Stop()
myHuman.WalkSpeed = 20
walkRandomly()
end
end
while wait(0.1) do
if myHuman.Health < 1 then
break
end
main()
end