My NPC keeps walking straight

Help!

My NPC keeps walking into a wall.

ezgif-1-64e3ffa2b25b

I have no clue what’s wrong with it.
Here is a script :

local PathfindingService = game:GetService("PathfindingService")

local watsher = script.Parent
local humanoid = watsher.Humanoid
local goal = game.Workspace.Neonblock

local function getpath(destination)
	local params = {
		["AgentHeight"] = 5,
		["AgentRadius"] = 2,
		["AgentCanJump"] = false
	}
	
	local path = PathfindingService:CreatePath(params)
	path:ComputeAsync(watsher.HumanoidRootPart.Position, destination.Position)
	
	return path	
end

local function walkTo(destination)
	local path = getpath(destination)
	
	for i,v in pairs(path:GetWaypoints()) do
		local parte = Instance.new("Part")
		parte.Parent = game.Workspace
		parte.Material = "Neon"
		parte.Anchored = true
		parte.CanCollide = false
		parte.Position = v.Position
		humanoid:MoveTo(v.Position)
		humanoid.MoveToFinished:Wait(2)
		
	end
end

while true do
	walkTo(goal)
end

You could try setting the watsher variable path to the HumanoidRootPart instead of the model.

Your problem is the while loop. You see everytime you make that loop, its creating more and more parts of the waypoint. That loop is unnecessary. All you need to do is a for loop through the waypoints, walk to each waypoint and when the for loop is finished, just throw in a line of code that makes the humanoid walk to the end destination. Not sure if I explained this well but basically, you’re constantly creating new waypoints because because of this while loop. If this still doesn’t make sense. I recommend watching TheDevKing’s video on PathfindingService on Youtube.

Hope this helps!

1 Like

I create a new save file with the same script and it works!
I want to know what happens to my old save file

Old save file :
Savefile

New save file :
Savefile2