I have a pathfinding script for a zombie and I have made it so that if there is nothing inbetween the zombie and the player then the zombie will move directly at the player but if there is something then it will pathfind to get to the player. It works for the most part , but if the player constantly switches between being behind an object and having a direct path to the zombie , the AI breaks and it starts looking like the zombie is lagging . Can someone tell me why this is? By the way the script is inside of the zombies model . Here it is :
local isMoving = false
local humanoid = script.Parent.Humanoid
local model = script.Parent
local PFS = game:GetService(“PathfindingService”)
local agroDistance = 150
local blackListedParts = {game.ReplicatedStorage}
local RS = game:GetService(“RunService”)
humanoid.MoveToFinished:Connect(function()
isMoving = false
end)
game.Workspace.ChildRemoved:Connect(function(child)
if child:FindFirstChild(“Humanoid”) then
for i,v in pairs(blackListedParts) do
if v.Parent == child then
table.remove(blackListedParts,i)
end
end
end
end)
game.Workspace.ChildAdded:Connect(function(child)
if child:FindFirstChild(“Humanoid”) then
for i=1,#blackListedParts do
if blackListedParts[i].Parent == child then
break
elseif i == #blackListedParts then
for a,b in pairs(child:GetChildren()) do
table.insert(blackListedParts,b)
end
end
end
end
end)
for i,v in pairs(model:GetChildren()) do
table.insert(blackListedParts,v)
end
while true do
wait()
local entireWorkSpace = game.Workspace:GetChildren()
local possibleTargets = {}
for i,v in pairs(entireWorkSpace) do
if v:FindFirstChild("Humanoid") and v.Parent ~= script.Parent then
possibleTargets[i] = v:FindFirstChild("UpperTorso")
end
end
local closestTarget = game.Workspace.referencePart
for i,v in pairs(possibleTargets) do
if (closestTarget.Position - model.UpperTorso.Position).Magnitude > (v.Position - model.UpperTorso.Position).Magnitude then
if v.Parent ~= model then
closestTarget = v
end
end
end
if closestTarget then
if (closestTarget.Position - model.HumanoidRootPart.Position).Magnitude <= agroDistance then
local maginitude = (closestTarget.Position - model.HumanoidRootPart.Position).Magnitude
local params = RaycastParams.new()
params.FilterDescendantsInstances = blackListedParts
params.FilterType = Enum.RaycastFilterType.Blacklist
local rayCastResults = game.Workspace:Raycast(model.UpperTorso.Position,(closestTarget.Position-model.UpperTorso.Position).Unit*(maginitude-1),params)
if rayCastResults then
-- humanoid:MoveTo(model.UpperTorso.Position)
print(rayCastResults.Instance)
local path = PFS:CreatePath()
path:ComputeAsync(model.UpperTorso.Position,closestTarget.Position)
local checkPoints = path:GetWaypoints()
for i,v in pairs(checkPoints) do
print(v.Position)
local newMag = (closestTarget.Position - model.HumanoidRootPart.Position).Magnitude
local newRayResults = workspace:Raycast(model.UpperTorso.Position,(closestTarget.Position-model.UpperTorso.Position).Unit*(newMag-1),params)
if newRayResults then
humanoid:MoveTo(v.Position)
humanoid.MoveToFinished:Wait()
else
break
end
end
else
humanoid:MoveTo(closestTarget.Position)
end
else
if isMoving == false then
humanoid:MoveTo(model.UpperTorso.Position + Vector3.new(math.random(-30,30),model.UpperTorso.Position.Y,math.random(-30,30)))
isMoving = true
end
end
end
end