I have a module script function that moves an AI using pathfinding using a custom method like so (modscript:MoveToPoint()). Its goal is to behave as the humanoid:MoveTo function but using pathfinding. However, one of the benefits of the MoveTo is that it can be interrupted by another call. How can I do this with the module script? (How can I interrupt a loop in a module script from the script that calls it?)

Outside code:

pathfinder:MoveToPoint(unit.HumanoidRootPart.Position, resource.PrimaryPart.Position + Vector3.new(0,20,0),0.16,1, unit)

Module Script Code:

function API:MoveToPoint(startPosition,endPosition,ratio,startIndex,model)
	--pathfinder was changed to API
	local torso = model.Torso

	local childs = model:GetChildren()
	for i = 1,#childs do
		if childs[i].ClassName == "Humanoid" then
			humanoid = childs[i]
		end
	end
	
		 path = API:FindPath(startPosition, endPosition)
				points = path:GetPointCoordinates()
				index  = path:CheckOcclusionAsync(startIndex)
				API:SetEmptyCutoff(ratio)

--			for pointnumber,pvector3 in pairs(points) do
--				part = Instance.new("Part")
--				part.Parent = Workspace				
--				part.Anchored = true
--				part.CanCollide = false
--				part.Size = Vector3.new(1,1,1)
--				part.CFrame = CFrame.new(pvector3)
--				part.Name = "PathfindingPart"
--			end				
				
			for pointnumber,pvector3 in pairs(points) do
				--print(pointnumber)
				--print(pvector3)
					if pvector3.y > torso.Position.y + 1 and ((pvector3.y - torso.Position.y) > 5) then   --		if point.y > mytorso.Position.y or ((point.y - mytorso.Position.y) > 5) then
						humanoid.Jump = true
						humanoid:MoveTo(pvector3)	
					end	
				humanoid:MoveTo(pvector3)
			repeat
				humanoid:MoveTo(pvector3)
				local distance = (torso.Position - pvector3).magnitude
				wait()
			until distance < 4
			end
end

3 comments

0

'break'

0

I want to do it from the script that calls it

0

make a function in the module that breaks it, then call it from the script that calls the module.