How can I fix the pausing in this NPC behavior script?

First of all, sorry for the unpractical and messy code, I’m a beginner.
Anyway, basically in my server script I made it so that where if you click on an NPC while it is roaming randomly in a set area it will follow you, indicated by a white beam. The remote event fires when a button is clicked and it will bring the NPC to another area.
Then I noticed that if you click the NPC while it is waiting to roam it won’t instant follow, instead actually waiting until that part loops.
I can’t seem to find a way to fix it. Help appreciated

local humanoid = script.Parent:WaitForChild("Humanoid")
local moveToPart = workspace.MovePart

local habitEvent = game.ReplicatedStorage.HabitAnimal

local currentHold = script.Parent:WaitForChild("CurrentHolder")

local beam = script.Parent:WaitForChild("OwnerBeam")

local detector = script.Parent:WaitForChild("ClickDetector")

local spaceBetween = 6
local followRadar = 50
local defaultSpeed = humanoid.WalkSpeed

local z_min = workspace.NaturalArea.Position.Z - (workspace.NaturalArea.Size.Z / 2)
local z_max = workspace.NaturalArea.Position.Z + (workspace.NaturalArea.Size.Z / 2)

local x_min = workspace.NaturalArea.Position.X - (workspace.NaturalArea.Size.X / 2)
local x_max = workspace.NaturalArea.Position.X + (workspace.NaturalArea.Size.X / 2)

detector.MouseClick:Connect(function(player)
	if currentHold.Value == "None" then
		currentHold.Value = player.Name
	end
end)

habitEvent.OnServerEvent:Connect(function(player)
	if currentHold.Value == player.Name then

		z_min = workspace.Floor.Position.Z - (workspace.Floor.Size.Z / 2)
		z_max = workspace.Floor.Position.Z + (workspace.Floor.Size.Z / 2)

		x_min = workspace.Floor.Position.X - (workspace.Floor.Size.X / 2)
		x_max = workspace.Floor.Position.X + (workspace.Floor.Size.X / 2)

		detector:Destroy()
		beam:Destroy()

		humanoid.Parent:WaitForChild("HumanoidRootPart").CFrame = workspace.Floor.CFrame

		currentHold.Value = "None"
	end
end)

--main behavior 
![2024-07-04 16-16-29|video](upload://pUomUXZIaMcvg7FXZ6wRflEq09U.mp4)
loop
while task.wait() do
	if currentHold.Value == "None" then
		detector.MaxActivationDistance = 32

		local x = math.random(x_min, x_max)
		local z = math.random(z_min, z_max)

		moveToPart.Position = Vector3.new(x, 0, z)

		humanoid:MoveTo(moveToPart.Position, moveToPart)
		humanoid.MoveToFinished:Wait()

		humanoid.WalkSpeed = defaultSpeed
		
		task.wait(3)
	else
		
		detector.MaxActivationDistance = 0

		local player = game.Players:FindFirstChild(currentHold.Value)
		local distance = (player.Character:WaitForChild("HumanoidRootPart").Position - humanoid.Parent:WaitForChild("HumanoidRootPart").Position).Magnitude
		
		humanoid:MoveTo(player.Character:WaitForChild("HumanoidRootPart").Position)

		beam.Attachment0 = player.Character:WaitForChild("HumanoidRootPart"):WaitForChild("RootAttachment")

		if distance > followRadar then
			humanoid.WalkSpeed = defaultSpeed * 2
			humanoid:MoveTo(Vector3.new(math.random(x_min, x_max), 0, math.random(z_min, z_max)))
			humanoid.MoveToFinished:Wait()
			beam.Attachment0 = nil
			currentHold.Value = "None"
		else
			humanoid.WalkSpeed = defaultSpeed
		end

		if distance < spaceBetween then
			humanoid.WalkSpeed = 0
		else
			humanoid.WalkSpeed = defaultSpeed
		end
	end
end

Bumping this just in case

charlimit

this is blocking your code, updates can only happen every 3 seconds, so if there is a change it might not be immediate.

How could I modify it without the pause?