Hello. I wrote a script in which the npc searches for the nearest object from the folder and ran to it. I used simplePath but it throws an error when I write BPath:Run(i).
local distanceTable = {}
local function distances()
for i, v in pairs(workspace.Objects:GetChildren()) do
local distanceFromPart = (BRootPart.Position - v.Position).magnitude
distanceTable[v.Name] = distanceFromPart
end
table.sort(distanceTable, function(ele1, ele2)
return ele1[2] > ele2[2]
end)
for i, v in pairs(distanceTable) do
print("Your distance from part "..i.." is "..v..".")
BPath:Run(i)
end
I’ve never used SimplePath but looking at the documentation, it looks like Run expects a Part and not a string(?) that you’re currently giving it. I’d get rid of that final for loop and just have something like
Thanks! It’s working. I will leave the full script if someone needs it.
local distanceTable = {}
local function distances()
for i, v in pairs(workspace.Objects:GetChildren()) do
local distanceFromPart = (BRootPart.Position - v.Position).magnitude
distanceTable[v.Name] = distanceFromPart
table.insert(distanceTable, {
part = v,
distance = distanceFromPart
})
end