MoveTo() Not stopping

Hi, i’m working in a mob system, Very basic, If the player is too far the mob will follow it, else if the player is too close it get damaged.
But there’s is a problem that makes it bad:

https://gyazo.com/af0654b072579411f29170a6c6b2c0a6

Script:

while task.wait() do
	for i, v in pairs(workspace:GetChildren()) do
		if v:IsA('Model') then
			if game.Players:FindFirstChild(v.Name) then
				local dist = (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude
				if dist <= 30 then
					script.Parent.Humanoid:MoveTo(v.HumanoidRootPart.Position)
				elseif dist <= 15 then
					script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
				end
			end
		end
	end
end

Humanoid:MoveTo (roblox.com)
looking through the documentation states there’s an 8-second delay before MoveTo() ends if the humanoid doesn’t reach its destination

is there any way possible to stop it?

I’m not sure.

local function Move(humanoid:Humanoid, targetPoint:Vector3)
	local targetReached = false

	-- listen for the humanoid reaching its target
	local connection
	connection = humanoid.MoveToFinished:Connect(function(reached)
		targetReached = true
		connection:Disconnect()
		connection = nil
	end)

	-- start walking
	humanoid:MoveTo(targetPoint)

	-- execute on a new thread so as to not yield function
	spawn(function()
		while not targetReached do
			-- does the humanoid still exist?
			if not (humanoid and humanoid.Parent) then
				break
			end
			-- has the target changed?
			if humanoid.WalkToPoint ~= targetPoint then
				break
			end
			-- refresh the timeout
			humanoid:MoveTo(targetPoint)
			wait(6)
		end

		-- disconnect the connection if it is still connected
		if connection then
			connection:Disconnect()
			connection = nil
		end
	end)
end

while task.wait() do	
	for i, v in pairs(workspace:GetChildren()) do
		if v and v:IsA("Model") and game.Players:FindFirstChild(v.Name) then
			local distance 
				= (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude
			if distance <= 30 then
				Move(script.Parent.Humanoid, v.HumanoidRootPart.Position)
			end
		end
	end
end

the problem still continue :confused: and in the same way as before

You can break it at the beginning like so

if dist <= 15 then
	break
end

if dist <= 30 then
	script.Parent.Humanoid:MoveTo(v.HumanoidRootPart.Position)
end

image

Still the same thing :confused:

Try this tutorial seems to work.

1 Like

ty ty ty!!! it helped me a lot, now i can work on more enemies :smiley:

1 Like