Why does this script work then fail

So I tried to make a NPC go across two points which worked then when I added the wall
and hit run it worked once then once at the second point just ran right into the wall.
Whats gone wrong?

local Solid = script.Parent
local humanoid = Solid.Humanoid

local PathfindingService = game:GetService("PathfindingService")

local function getPath(destination)
	local pathParams = {
		["AgentHeight"] = 5.4,
		["AgentRadius"] = 3,
		["AgentCanJump"] = false
	}
	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(Solid.HumanoidRootPart.Position, destination.Position)
	
	return path
end


local function walkTo (destination)
	local path = getPath(destination)

for index, waypoint in pairs(path:GetWaypoints()) do 
	humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait()
	end

while true do 
	humanoid:MoveTo(workspace.destination.Position)
	humanoid.MoveToFinished:Wait()
	humanoid:MoveTo(workspace.destination2.Position)
	humanoid.MoveToFinished:Wait()
	end
end


while true do 
	walkTo(workspace.destination)
	walkTo(workspace.destination2)
end

Idk if this will help, but I see an error. You put two “while true do” loops. Nothing can run after a “while true do” loop so it is physically impossible to have two of them in one script, unless one of them runs in a function I think.

Changed the while true do

local solid = script.Parent
local humanoid = solid.Humanoid

local PathfindingService = game:GetService("PathfindingService")

local function getPath(destination)
	local pathParams = {
		["AgentHeight"] = 5.4,
		["AgentRadius"] = 3,
		["AgentCanJump"] = false
	}
	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(solid.HumanoidRootPart.Position, destination.Position)
	
	return path
end


local function walkTo (destination)
	local path = getPath(destination)

for index, waypoint in pairs(path:GetWaypoints()) do 
	humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait()
	end

while true do 
		walkTo(workspace.destination)
		walkTo(workspace.destination2)
	end
end

I printed and it fails at the first function

local function getPath(destination)

the problem is that you forgot to put an “end” after the function, and it gets messy with your loops

local Solid = script.Parent
local humanoid = Solid.Humanoid

local PathfindingService = game:GetService("PathfindingService")

local function getPath(destination)
	local pathParams = {
		["AgentHeight"] = 5.4,
		["AgentRadius"] = 3,
		["AgentCanJump"] = false
	}
	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(Solid.HumanoidRootPart.Position, destination.Position)
	
	return path
end


local function walkTo (destination)
	local path = getPath(destination)

for index, waypoint in pairs(path:GetWaypoints()) do 
	humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait()
end

while true do 
	humanoid:MoveTo(workspace.destination.Position)
	humanoid.MoveToFinished:Wait()
	humanoid:MoveTo(workspace.destination2.Position)
	humanoid.MoveToFinished:Wait()
end


while true do 
	walkTo(workspace.destination)
	walkTo(workspace.destination2)
end
end

also to fix the loop problems, wrap them in a spawn() function, like this:


spawn(function()
      loop
end)

spawn(function()
      loop
end)