How do I stop pathfinding after some time?

I’m using pathfinding and I need to know how to stop it after a certain amount of time
How would I do this?

local function search()
	
	local destination = game.Players:GetChildren()[1].Character:FindFirstChild('HumanoidRootPart').Position

	-- create path object
	local path = pathfindingService:CreatePath()

	-- compute a path
	path:ComputeAsync(body.Position, destination)

	-- get the waypoints table
	local waypoints = path:GetWaypoints()
	
	for k, waypoint in pairs(waypoints) do
		humanoid:MoveTo(waypoint.Position)

		-- change humanoid state to jump if necessary
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end

		humanoid.MoveToFinished:Wait()
	end
	
	
end

Thats my code

You can return the function after what you want is complete.

I would add a variable taht at the start is false, like:

local started = false

And then when needed make it true, something. Like:

—this goes at the end started = true

And at the start do

if started = true then
wait(time)
script.Enabled = false
else
—your code