Problem With Canceling MoveTo()

I currently have a spider that when in range follows the player when the spider gets close enough to the player the spider should stop MoveTo() and do an attack. but the problem is when I call MoveTo Towards the Spider’s own humanoid RootPart to stop movement it flicks to the left side any solutions would help.
including other ways to stop movement.

I added comments where the move to part is

if anyone is wondering I already tried anchoring and un anchoring.


while wait(1) do
	for _, Spider in pairs(SpiderFolder:GetChildren()) do
		
		local Character = nil
		local Magnitude = nil
		local LastTargetDist = SpiderDetectDistance
		local ClosestTarget = nil
		if Spider.StartPos.Value == "" then
			Spider.StartPos.Value = tostring(Spider.HumanoidRootPart.Position)
		end
		for _, Player in pairs(game.Players:GetChildren()) do
			Character = game.Workspace:WaitForChild(Player.Name)
			Magnitude = (Character:WaitForChild("HumanoidRootPart").Position - Spider.HumanoidRootPart.Position).Magnitude
			if Magnitude < LastTargetDist then
				LastTargetDist = Magnitude
				ClosestTarget = Character
			end
		end
		
		if ClosestTarget then
			if Spider.Targeted.Value == false then
				local SpiderWalk = Spider.Zombie:LoadAnimation(Spider.Walk)
				local SpiderBite = Spider.Zombie:LoadAnimation(Spider.Bite)
				Spider.Targeted.Value = true
				Spider.HumanoidRootPart:SetNetworkOwner(game.Players:FindFirstChild(ClosestTarget.Name))
				Spider.LastPosition.Value = tostring(Vector3.new(0,1000,0))
				
				coroutine.resume(coroutine.create(function()
					while RunService.Heartbeat:Wait() do
						local CharacterDist = (ClosestTarget:WaitForChild("HumanoidRootPart").Position - Spider.HumanoidRootPart.Position).Magnitude
						local CharacterFromSpawn = (ClosestTarget:WaitForChild("HumanoidRootPart").Position - Vector3.new(Spider.StartPos.Value:match("(.+), (.+), (.+)"))).Magnitude
						local SpawnPosition = Vector3.new(Spider.StartPos.Value:match("(.+), (.+), (.+)"))
						local CharacterPosition = ClosestTarget.HumanoidRootPart.Position
						local LastPosition = Vector3.new(Spider.LastPosition.Value:match("(.+), (.+), (.+)"))
						local PointToPoint = (CharacterPosition - LastPosition).Magnitude
						if CharacterFromSpawn < SpiderDetectDistance then
								if CharacterDist > 10 then --distance from spider to character 
									if PointToPoint > 2 then -- dont worry about this its just to moveto() is not spammed 
										if Spider.Walking.Value == false then
											Spider.Walking.Value = true
											SpiderWalk:Play()
										end
										Spider.Zombie:MoveTo(CharacterPosition) -- move to player 
										Spider.LastPosition.Value = tostring(ClosestTarget.HumanoidRootPart.Position) -- again dont worry 
									end
								else
									if Spider.Walking.Value == true then
										Spider.Zombie:MoveTo(Spider.Torso.Position)
										Spider.Walking.Value = false
										SpiderWalk:Stop()
									end
									
								end
						else
							
							Spider.Zombie:MoveTo(SpawnPosition)
							repeat
							wait(.1)
							until game.Workspace.Monters.Spiders.Spider.HumanoidRootPart.Velocity == Vector3.new(0,0,0)
							if Spider.Walking.Value == true then
								Spider.Walking.Value = false
								SpiderWalk:Stop()
							end
							Spider.HumanoidRootPart.Position = SpawnPosition
							Spider.Targeted.Value = false
							break
						end
					end
				end))
			end	
		end
	end
end
1 Like

Could you post the code for what happens after the MoveTo is stopped?

So I have some updates on the problem what’s causing the flicks is the auto-rotate inside the humanoid so I figured out what causes it, it was Spider.Zombie:MoveTo(Spider.Torso.Position) used to move the spider to a stop but when I used that for some reason it tries moving backward hense the flick (trying to turn around) so anyone know why it’s moving backward like that when trying to put to a stop

You still have issues right or not?

I do just need a solution to stop it from moving backwards once reaching the target

When I used MoveTo()

This is my sample code

Character:MoveTo(Part.Position, Part)

If MoveTo() does not work

Then use CFrame

1 Like

You can fix this be disabling AutoRotate. To get it to work using your method, set it to false before you set its move position to its rootpart, then you’ll have to wait a second (or it’ll rotate anyway due to how Roblox moving works), then set it to true again. Alternatively, you can permanently disable it and do the rotation automatically client-side (as the server shouldn’t need to know the direction it’s supposed to be facing, but it’s up to you).

1 Like

so actually that second parameter ended up fixing not sure why it would, but here the new line of code thanks man Spider.Zombie:MoveTo(CharacterPosition, ClosestTarget.HumanoidRootPart)

2 Likes

Ah yes, that’d be an even better solution. I was just thinking about that too. :+1:

out of curiosity what does that second parameter even do

I haven’t tested, but if it works anything like CFrame, it probably gives a look direction.

Its the destination of what object you want to move to its on the second parameter