Hello, I was working recently on an NPC that’s supposed to move & walk in a store as an employee, and the only way that I know to move an NPC or a character is PathfindingService but the problem is after I finished all the scripting stuff with the Invisible Parts and Pathfinding Modifiers, there was a problem that the NPC is sometimes walking into the walls at specific points and not re-calculating the path in a correct way.
That’s an Image showing what happens:
I tried searching on all the Devforum and the DevHub to find a solution but none worked.
local RandomChosenRestock = RestockWaypoints[math.random(1,#RestockWaypoints)]
if RandomChosenRestock:IsA("BasePart") then
local success, errorMessage = pcall(function()
path:ComputeAsync(humanoidRootPart.Position,RandomChosenRestock.Position)
end)
local currentRestockWaypoint = 0
if success then
boxThings(path,currentRestockWaypoint,RandomChosenRestock,TweenIO)
else
local success, errorMessage = pcall(function()
path:ComputeAsync(humanoidRootPart.Position,RandomChosenRestock.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
boxThings(path,currentRestockWaypoint,RandomChosenRestock,TweenIO)
end
end
end
The function part:
for i,RestockPathWaypoint in pairs(PathWaypoints) do
if currentRestockWaypoint < #PathWaypoints then
idle1:Stop()
idle2:Stop()
humanoid:MoveTo(RestockPathWaypoint.Position)
if not walkAnim.IsPlaying then
walkAnim:Play()
end
humanoid.MoveToFinished:Wait()
currentRestockWaypoint += 1
end
Alvinblox had the same problem with pathfinding in this post. (Although I don’t think he ever solved the problem either).
I’ve also had issues with pathfinding in the past, and I think your best bet would be to use A* pathfinding and generate nodes around the store, removing nodes that are inside or too close to furniture.
You can find a working example implemented in Roblox here.
For a more detailed explanation on how A* pathfinding works, I recommend reading this article.
everywhere I used currentRestockWaypoint:
the code continues here but I didn’t put it because there is things that aren’t related to the topic
local PathWaypoints = path:GetWaypoints()
for i,RestockPathWaypoint in pairs(PathWaypoints) do
if currentRestockWaypoint < #PathWaypoints then
idle1:Stop()
idle2:Stop()
humanoid:MoveTo(RestockPathWaypoint.Position)
if not walkAnim.IsPlaying then
walkAnim:Play()
end
humanoid.MoveToFinished:Wait()
currentRestockWaypoint += 1
if currentRestockWaypoint >= #PathWaypoints then
local orientation = RestockOrientations[RandomChosenRestock.Name]
local newCF = humanoidRootPart.CFrame * CFrame.Angles(math.rad(0),math.rad(orientation.Value.Y-humanoidRootPart.Orientation.Y),math.rad(0))
walkAnim:Stop()
currentRestockWaypoint = 0
second part:
RandomChosenRestock is another table of waypoints so don’t worry about it
if RandomChosenRestock:IsA("BasePart") then
local currentRestockWaypoint
if computePaths() then
local waypoints = path:GetWaypoints()
currentRestockWaypoint = 1
for i,waypoint in pairs(waypoints) do
if currentRestockWaypoint < #waypoints then
humanoid:MoveTo(waypoint.Position)
currentRestockWaypoint += 1
if not walkAnim.IsPlaying then
walkAnim:Play()
end
humanoid.MoveToFinished:Wait()
else
walkAnim:Stop()
idle1:Play()
idle2:Play()
local function moveNPCtoRestock()
local success, errorMessage = pcall(function()
path:ComputeAsync(humanoidRootPart.Position,RandomChosenRestock.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
boxThings(path,currentRestockWaypoint,RandomChosenRestock,TweenIO)
else
local success, errorMessage = pcall(function()
path:ComputeAsync(humanoidRootPart.Position,RandomChosenRestock.Position)
end)
moveNPCtoRestock()
end
end
moveNPCtoRestock()
end
end
end
end
I assume you’ve already tried increasing this to 4 or 6, maybe even 10?
I honestly have no idea why this is happening. I can’t reproduce this strange bug. Though, something that I do find strange is that WaypointSpacing is 2, but in the image the waypoints are much further apart.
The only thing I think could be happening is that the NPCs are far bigger than regular characters, though I doubt they are. I think you’re better off making a custom node-based pathfinding implementation.