I’ve been wondering if there is much more improvement to optimise this code for npc pathfinding,
If so please tell me, thanks in advance
local pathfindservice = game:GetService("PathfindingService")
local lastattack = 0
local pathmade = false
local target = nil
local ally = nil
local ignoretable = {}
local players = game:GetService("Players")
local par = RaycastParams.new()
local enemy = nil
local enemyteam = nil
table.insert(ignoretable,1,npc)
par.FilterDescendantsInstances = ignoretable
par.FilterType = Enum.RaycastFilterType.Blacklist
local function makepath()
pathmade = true
local raycast = workspace:Raycast(npc.PrimaryPart.Position,CFrame.new(npc.PrimaryPart.Position,target.Position).lookVector * (8 * npc.Humanoid.WalkSpeed),par)
if raycast then
if raycast.Instance.Parent == target.Parent then
local check
check = target ~= nil and target.Parent ~= nil and target.Parent.Humanoid.Health > 0
if check then
npc.Humanoid:Move(CFrame.new(npc.PrimaryPart.Position,target.Position).lookVector)
end
else
local path = pathfindservice:CreatePath()
path:ComputeAsync(npc.PrimaryPart.Position,target.Position)
if path.Status == Enum.PathStatus.Success then
for _,v in ipairs(path:GetWaypoints()) do
local ifcheck
ifcheck = target ~= nil and target.Parent ~= nil and target.Parent.Humanoid.Health > 0 and (target.Parent.PrimaryPart.Transparency < 0.75 or (npc.PrimaryPart.Position - target.Parent.PrimaryPart.Position).Magnitude < 30)
if ifcheck then
local ray = workspace:Raycast(npc.PrimaryPart.Position,CFrame.new(npc.PrimaryPart.Position,target.Position).lookVector * (8 * npc.Humanoid.WalkSpeed),par)
local ifcheck2 = ray and ray.Instance.Parent == target.Parent
if ifcheck2 then
break
else
if v.Action == Enum.PathWaypointAction.Walk then
npc.Humanoid:MoveTo(v.Position)
npc.Humanoid.MoveToFinished:Wait()
else
npc.Humanoid.Jump = true
end
end
end
end
end
end
end
pathmade = false
end
game["Run Service"].Heartbeat:Connect(function()
if npc.Parent == game.Workspace.DefenderNpc then
enemy = workspace.AttackerNpc
enemyteam = game.Teams.Attackers
ally = workspace.DefenderNpc
elseif npc.Parent == game.Workspace.AttackerNpc then
enemy = workspace.DefenderNpc
enemyteam = game.Teams.Defenders
ally = workspace.AttackerNpc
end
table.insert(ignoretable,2,ally)
par.FilterDescendantsInstances = ignoretable
local lowestdistance = 2500
for _,v in ipairs(enemyteam:GetPlayers()) do
local check = v.Character.PrimaryPart ~= nil and v.Character:FindFirstChild("Humanoid").Health > 0
if check then
local detectrange = (50 * (1 - v.Character.Torso.Transparency))
local detectcheck
detectcheck = (v.Character.Torso.Position - npc.PrimaryPart.Position).Magnitude < lowestdistance and (v.Character.Torso.Transparency < 0.75 or (npc.PrimaryPart.Position - v.Character.Torso.Position).Magnitude < detectrange)
if detectcheck then
lowestdistance = (v.Character.Torso.Position - npc.PrimaryPart.Position).Magnitude
target = v.Character.Torso
end
end
end
for _,v in ipairs(enemy:GetChildren()) do
local check
check = v ~= nil and v.Parent ~= nil and v:IsA("Model") and v.PrimaryPart ~= nil and v:FindFirstChild("Humanoid")
if check then
local check2
local detectrange = (50 * (1 - v.PrimaryPart.Transparency))
check2 = v.Humanoid.Health > 0 and (npc.PrimaryPart.Position - v.PrimaryPart.Position).Magnitude < 2500 and (npc.PrimaryPart.Position - v.PrimaryPart.Position).Magnitude < lowestdistance and (v.PrimaryPart.Transparency < 0.75 or (npc.PrimaryPart.Position - v.PrimaryPart.Position).Magnitude < detectrange)
if check2 then
lowestdistance = (npc.PrimaryPart.Position - v.PrimaryPart.Position).Magnitude
target = v.PrimaryPart
end
end
end
if target ~= nil and enemy ~= nil then
if pathmade == false then
local newpath = coroutine.wrap(makepath)
newpath()
end
local targetcheck
targetcheck = target ~= nil and target.Parent ~= nil and target.Parent.Humanoid.Health > 0 and (npc.PrimaryPart.Position - target.Position).Magnitude < 2500 and npc.Humanoid.Health > 0 and npc.PrimaryPart.Anchored == false and time() - lastattack > npc.AttackCooldown.Value
if targetcheck then -- attack
end
end
table.remove(ignoretable,2)
end)