The problem is that once the blob finds a piece of food the path is continuously looped to that position even if it’s been destroyed. Ideally, the blob would find the next closest piece of food. I came across this post where another dev was having a similar problem. I tried to implement the solution but apparently, I’m not doing something correctly because it’s still not working.
AI is using a fork of SimplePath
Code:
local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)
local Blob = script.Parent
local Path = SimplePath.new(Blob)
local Goal
---
local FoodFolder = workspace.Food
local Closest
local BlobPos = Blob.PrimaryPart.Position
---
local function GenGoal()
if unpack(FoodFolder:GetChildren()) ~= nil then
for i,v in pairs(FoodFolder:GetChildren()) do
if Closest == nil then
Closest = v
Goal = Closest
else
if (BlobPos - v.Position).magnitude < (Closest.Position - BlobPos).magnitude then
Closest = v
Goal = Closest
end
end
end
end
end
Path.Visualize = true
Blob.Body.Touched:Connect(function(hit)
if hit.Name == "Food" then
hit:Destroy()
end
end)
GenGoal()
Path:Run(Goal)
while true do
task.wait()
if Goal ~= nil then
GenGoal()
Path:Run(Goal)
end
end