Issue With :MoveTo() Lagging

Hey there!

Recently I’ve been working on a horror game with a very basic chaser monster. The issue comes when the monster comes close to the player, in which case it seems to lag while running towards the player. When it’s farther away it seems moves smoothly, but as it gets closer its movement lags. Any help is greatly appreciated!

Here’s the code:

local anim0 = Instance.new("Animation")
anim0.AnimationId = "rbxassetid://83535634079542"
local anim1 = Instance.new("Animation")
anim1.AnimationId = "rbxassetid://92627531934333"
local anim2 = Instance.new("Animation")
anim2.AnimationId = "rbxassetid://101023883219742"
local anim3 = Instance.new("Animation")
anim3.AnimationId = "rbxassetid://97225510963888"

local animateidle = script.Parent.Humanoid.Animator:LoadAnimation(anim0)	
local animatewalk = script.Parent.Humanoid.Animator:LoadAnimation(anim1)
local animaterun = script.Parent.Humanoid.Animator:LoadAnimation(anim2)
local animatesound = script.Parent.Humanoid.Animator:LoadAnimation(anim3)

local idling = false
local walking = false
local running = false

function talk()

	while true do

		animatesound:AdjustSpeed(0.125)
		animatesound:Play()
		script.Parent.HumanoidRootPart.Cry.PitchShiftSoundEffect.Octave = Random.new():NextNumber(0.25, 0.75)
		script.Parent.HumanoidRootPart.Cry:Play()
		task.wait(math.random(5, 30))

			end

		end

local coro = coroutine.create(talk)

coroutine.resume(coro)

function idle()

	idling = true
	walking = false
	running = false

	animateidle:Play()
	animatewalk:Stop()
	animaterun:Stop()
	
	script.Parent.Humanoid:Move(Vector3.new(0, 0, 0))

		end

function walk()
	
	local direction = (script.Parent.HumanoidRootPart.Position - Vector3.new(0, 500, 0)) - script.Parent.HumanoidRootPart.Position
	local ray = workspace:Raycast(script.Parent.HumanoidRootPart.Position, direction)
	
	idling = false
	walking = true
	running = false
	
	animateidle:Stop()
	animatewalk:Play()
	animaterun:Stop()

	script.Parent.Humanoid.WalkSpeed = 16
	
	script.Parent.HumanoidRootPart.Footstep.Volume = 2.5
	
	local function footsteps()
		
		while walking == true do

			script.Parent.HumanoidRootPart.Footstep.PitchShiftSoundEffect.Octave = Random.new():NextNumber(0.75, 1.25)
			script.Parent.HumanoidRootPart.Footstep:Play()
			task.wait(1)

				end
		
			end
	
	local coro = coroutine.create(footsteps)
	
	coroutine.resume(coro)
	
		end

function run()

	idling = false
	walking = false
	running = true

	animateidle:Stop()
	animatewalk:Stop()
	animaterun:Play()

	script.Parent.Humanoid.WalkSpeed = 32
	
	script.Parent.HumanoidRootPart.Footstep.Volume = 7.5

	local function footsteps()

		while running == true do

			script.Parent.HumanoidRootPart.Footstep.PitchShiftSoundEffect.Octave = Random.new():NextNumber(0.75, 1.25)
			script.Parent.HumanoidRootPart.Footstep:Play()
			task.wait(0.5)

				end

			end

	local coro = coroutine.create(footsteps)

	coroutine.resume(coro)

		end

function search()

	local destination = Vector3.new(0, 0, 0)

	local direction = destination - script.Parent.HumanoidRootPart.Position

	local ray = game.Workspace:Raycast(script.Parent.HumanoidRootPart.Position, direction)

	local plr = script.Parent.HumanoidRootPart

	--find a player (MUST BE LOOPED IN COROUTINE SO WE DON'T STOP LOOKING FOR PLAYERS AFTER FIRST GO)
	local function searchforplr()
		
		while true do

			for i, v in pairs(game.Workspace:GetChildren()) do

				if v:FindFirstChild("Humanoid") ~= nil and v ~= script.Parent then

					destination = v.HumanoidRootPart.Position

					direction = destination - script.Parent.HumanoidRootPart.Position

					ray = game.Workspace:Raycast(script.Parent.HumanoidRootPart.Position, direction)

					if ray.Instance.Parent:FindFirstChild("Humanoid") ~= nil then

						plr = ray.Instance.Parent.HumanoidRootPart
						print(ray.Instance.Parent.Name .. " was found")
						break

					else
						
						print("no players found")
						
							end

						end

					end
			
			task.wait(0.25)
			
				end
		
			end

	--attack the player we found (ALSO HAS TO BE LOOPED IN COROUTINE)
	local function chase()

	--	if ray.Instance.Parent:FindFirstChild("Humanoid") ~= nil then

			run()
			
			while true do
				
				print(plr.Position)
				script.Parent.Humanoid:MoveTo(plr.Position)

				task.wait(0.25)
	
					end
			
				end
		
		--	end

	local coro0 = coroutine.create(searchforplr)

	coroutine.resume(coro0)

	local coro1 = coroutine.create(chase)

	coroutine.resume(coro1)

		end

idle()
search()

Here’s a video of the issue: