What's wrong with my Pathfinding script?

So I’ve made a script where NPCs will spawn, look at something and then go to the register, when the player clicks the register the NPC at the front of the queue will leave. My issue really is that sometimes it works and sometimes it doesn’t, no errors are being thrown so I can’t really figure out the problem.

I’ve created a script inside the NPC to handle most of the movement and checks, then used a ModuleScript to create a table that monitors the queue.

Script inside the NPC:

cm = require(game:GetService("ServerScriptService").CustomerManager)

NPC = script.Parent
Owner = script.Parent:GetAttribute("Owner")
readytoQueue = false
timetoLeave = false
left = false

local pfs = game:GetService("PathfindingService")

local path = pfs:CreatePath({
	Costs = {
		RestrictedZone = math.huge
	}
})

function followPath(goal, path)
	
	path:ComputeAsync(NPC.HumanoidRootPart.Position, goal.Position)
	wps = {}
	if path.Status == Enum.PathStatus.Success then
		wps = path:GetWaypoints()
		wpIdx = 1
		NPC.Humanoid:MoveTo(wps[wpIdx].Position)
	end
	
end

function Startup()
	
	if workspace.Tycoons:FindFirstChild(Owner.."_Tycoon") then
		local Tycoon = workspace.Tycoons:FindFirstChild(Owner.."_Tycoon")
		local npc_waypoints = {}

		for i, v in pairs(Tycoon.Customer_Waypoints:GetChildren()) do
			if v.Name == "Waypoint" then
			
				table.insert(npc_waypoints, v.CFrame)

			end
		end
		
		local selectWaypoint = npc_waypoints[math.random(1, #npc_waypoints)]
		followPath(selectWaypoint, path)
		
		NPC.Humanoid.MoveToFinished:Connect(function(reached)
			if reached and wpIdx < #wps then
				wpIdx += 1
				NPC.Humanoid:MoveTo(wps[wpIdx].Position)
			else
				if reached and left == true then
					NPC:Remove()
				elseif reached and timetoLeave == true then
					NPC:Remove()
				elseif reached and readytoQueue == false then
					readytoQueue = true
					local addQueue = cm.AddQueue(Owner, NPC)
					local findQueue = table.find(cm.FindQueue(Owner), NPC)
					followPath(Tycoon.Customer_Waypoints.Queue[findQueue], path)
				elseif reached and readytoQueue == true then
					repeat
						local findQueue = table.find(cm.FindQueue(Owner), NPC)
						if findQueue ~= nil then
							print(findQueue)
							followPath(Tycoon.Customer_Waypoints.Queue[findQueue], path)
							if findQueue == 1 then
								script.Parent.Head.Checkout.Enabled = true
							end
						end
						wait(1)
					until findQueue == nil
					print("leaving")
					followPath(Tycoon.Customer_Waypoints.Exit, path)
					timetoLeave = true
				
				end
			end
		end)

		path.Blocked:Connect(function(blockedWpIdx)
			if blockedWpIdx > wpIdx then
				print("yeah..")
				followPath(npc_waypoints[1])
			end
		end)
	end
	
end

function exit()
	
end

Startup()

ModuleScript in ServerScriptService

local CustomerManager = {}
ServerStorage = game:GetService("ServerStorage")

customerQueues = {}

function CustomerManager.StartSession(player)
	
	local Tycoons = game.Workspace.Tycoons
	if Tycoons:FindFirstChild(player.UserId.."_Tycoon") then
		local Tycoon = Tycoons:FindFirstChild(player.UserId.."_Tycoon")
		local MyQueue = {}
		customerQueues[player.UserId] = MyQueue
		createCustomers(player, Tycoon, 3)
	end
	
end

function createCustomers(player, Tycoon, amount)
	for i = 1, amount do

		local npcClone = ServerStorage.NPC:Clone()
		npcClone:SetPrimaryPartCFrame(Tycoon.CustomerSpawn.CFrame)
		npcClone.Name = "NPC_"..i
		npcClone:SetAttribute("Owner", player.UserId)
		npcClone.Parent = Tycoon.Customers
		wait(math.random(5,10))

	end
end

function CustomerManager.AddQueue(playerID, NPC)
	
	table.insert(customerQueues[playerID], NPC)
	return(customerQueues[playerID])
	
end

function CustomerManager.FindQueue(playerID)
	return(customerQueues[playerID])
end

function CustomerManager.RemoveQueue(player, NPC)
	
	if customerQueues[player.UserId][1] then
		table.remove(customerQueues[player.UserId], 1)
	end

end



return CustomerManager

Any input would be apricated :slight_smile: