My NPC keeps walking into a wall like this
Code:
while true do
local closest = math.huge
local targetPos = nil
for _, player in pairs(Players:GetPlayers()) do
if player.Character == nil then
continue
end
if (player.Character:GetPivot().Position-script.Parent:GetPivot().Position).Magnitude < closest then
closest = (player.Character:GetPivot().Position-script.Parent:GetPivot().Position).Magnitude
targetPos = player.Character:GetPivot().Position
end
end
if targetPos == nil then
targetPos = workspace._MovePoints:GetChildren()[math.random(1, #workspace._MovePoints:GetChildren())].Position
end
local path = PathfindingService:CreatePath({
AgentRadius = 10,
AgentHeight = 15,
Costs = {
Wall = math.huge
}
})
local success, errormessage = pcall(function()
path:ComputeAsync(script.Parent:GetPivot().Position, targetPos)
end)
if success and path.Status == Enum.PathStatus.Success then
local part = Instance.new("Part")
part.Size = Vector3.new(1,1,1)
part.CanCollide = false
part.Anchored = true
part.Parent = workspace
part.Position = path:GetWaypoints()[2].Position
script.Parent.Humanoid:MoveTo(path:GetWaypoints()[2].Position)
local cont = false
task.spawn(function()
script.Parent.Humanoid.MoveToFinished:Wait()
cont = true
end)
path.Blocked:Connect(function()
cont = true
end)
task.delay(3, function()
cont = true
end)
repeat task.wait() until cont
else
warn(errormessage)
end
task.wait()
end