Help with Humanoid:MoveTo()

I am trying to create a mailman job, where the player’s character walks to a point then plays an animation but, if the character walks somewhere else while the character is walking to the point on the previous mailbox then goes back to the point again, the next mailbox the character triggers teleports to the previous mailbox. How can I fix this?

local cooldown = false
local cooldown2 = false
local connection 
local connection2 
local tweenService = game:GetService("TweenService")

local tweenInformation = TweenInfo.new(0.5,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,false,0)
local DataManager = require(game.ReplicatedStorage.DataManager)
local walkpart = script.Parent.Parent.Part


script.Parent.Triggered:Connect(function(plr)
	local player = plr
	if not cooldown and player.PlayerGui.Task.Tasks.Value == "Mail" and player.PlayerGui.Task.Tasks.Amount.Value == 1  then
		local Character = plr.Character
		Character.Humanoid:MoveTo(walkpart.Position)
		connection = Character.Humanoid.MoveToFinished:Connect(function(reached)
			if not cooldown2  then
				local Anim = Instance.new("Animation")
				Anim.AnimationId = 'rbxassetid://7794700511'
				local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
				cooldown2 = true
				cooldown = true
				if Character.Humanoid.WalkToPoint == walkpart.Position then
					Character.HumanoidRootPart.Anchored = true
					Character.HumanoidRootPart.CFrame = walkpart.CFrame  * CFrame.new(0,4.85,0)
					local tween1 = tweenService:Create(Character.HumanoidRootPart,tweenInformation,{CFrame = CFrame.new(Character.HumanoidRootPart.Position) * CFrame.Angles(0,Vector3.new(CFrame.new(Character.HumanoidRootPart.Position, script.Parent.Parent.Position):ToOrientation()).Y,0)})
					tween1:Play()
					wait(.1)
					PlayAnim:Play()
				end
				connection2 = PlayAnim:GetMarkerReachedSignal("deliver"):Connect(function (paramString)
					
					cooldown2 = true
					wait(.5)
					if connection then
						connection:Disconnect()
						connection = nil
					end
					if connection2 then
						connection2:Disconnect()
						connection2 = nil
					end
					if player.PlayerGui.Task.Tasks.Amount.Value == 1 and Character.Humanoid.WalkToPoint == walkpart.Position then
						local gui = player.PlayerGui.Task:FindFirstChild("Mail")
						Character.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
						Character.Humanoid.WalkToPart = nil
						Character.HumanoidRootPart.Anchored = false
						if gui then
							local data = DataManager:Get(player)
							if player.PlayerGui.Task.Tasks.Amount.Value == 1 then
								if data then
									if player then
										game.ReplicatedStorage.sound:FireClient(player)
										data.cash+=50
										data.exp += 10
									end
								end

								local rand = math.random(1,5)
								if rand == 1 then
									player.PlayerGui.Task.Tasks.Amount.Value = 2
									gui.Adornee = workspace.Mail.mailpart2
									game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
								end
								if rand == 2 then
									player.PlayerGui.Task.Tasks.Amount.Value = 3
									gui.Adornee = workspace.Mail.mailpart3
									game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
								end
								if rand == 3 then
									player.PlayerGui.Task.Tasks.Amount.Value = 4
									gui.Adornee = workspace.Mail.mailpart4
									game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
								end
								if rand == 4 then
									player.PlayerGui.Task.Tasks.Amount.Value = 5
									gui.Adornee = workspace.Mail.mailpart5
									game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
								end
								if rand == 5 then
									player.PlayerGui.Task.Tasks.Amount.Value = 6
									gui.Adornee = workspace.Mail.mailpart6
									game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
								end

							end
							
						end
					end
					
					wait(5)
					cooldown2 = false
					cooldown = false
						
					
					
				end)
			end


			wait(8)
			if connection then
				Character.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
				connection:Disconnect()
				connection = nil
			end
		end)
	end

end)

I’m not sure how helpful or viable this is going to be but possibly looping the :MoveTo() so it’s constantly moving to the point. It’s a hacky solution but I believe it will work. I can’t think of anything else at the moment unfortunately.

1 Like

I am actually having the same issue with my combat game. When I tween the HRP as an impact results to the NPC teleporting back to the original tween start point while the tweens reversal properties are unticked and the moveto is managed to not run during the tween. (the npc also is anchored so no possible movement during tween)

Oh, unfortunately, I’m not that skilled with moving NPC so I’m not sure what the problem could be. Hopefully, someone is able to help, sorry that this wasn’t helpful.

I fixed it.

local cooldown = false
local cooldown2 = false
local connection 
local connection2 
local tweenService = game:GetService("TweenService")

local tweenInformation = TweenInfo.new(0.5,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,false,0)
local DataManager = require(game.ReplicatedStorage.DataManager)
local walkpart = script.Parent.Parent.Part


script.Parent.Triggered:Connect(function(plr)
	local player = plr
	if not cooldown and player.PlayerGui.Task.Tasks.Value == "Mail" and player.PlayerGui.Task.Tasks.Amount.Value == 1  then
		local Character = plr.Character
		Character.Humanoid:MoveTo(walkpart.Position)
		connection = Character.Humanoid.MoveToFinished:Connect(function(reached)
			if not cooldown2 and Character.Humanoid.WalkToPoint == walkpart.Position and player.PlayerGui.Task.Tasks.Value == "Mail" and player.PlayerGui.Task.Tasks.Amount.Value == 1  then
				local Anim = Instance.new("Animation")
				Anim.AnimationId = 'rbxassetid://7794700511'
				local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
				cooldown2 = true
				cooldown = true
				if Character.Humanoid.WalkToPoint == walkpart.Position then
					Character.HumanoidRootPart.Anchored = true
					Character.HumanoidRootPart.CFrame = walkpart.CFrame  * CFrame.new(0,4.85,0)
					local tween1 = tweenService:Create(Character.HumanoidRootPart,tweenInformation,{CFrame = CFrame.new(Character.HumanoidRootPart.Position) * CFrame.Angles(0,Vector3.new(CFrame.new(Character.HumanoidRootPart.Position, script.Parent.Parent.Position):ToOrientation()).Y,0)})
					tween1:Play()
					wait(.1)
					PlayAnim:Play()
				end
				connection2 = PlayAnim:GetMarkerReachedSignal("deliver"):Connect(function (paramString)
					
					cooldown2 = true
					wait(.5)
					if connection then
						connection:Disconnect()
						connection = nil
					end
					if connection2 then
						connection2:Disconnect()
						connection2 = nil
					end
					if player.PlayerGui.Task.Tasks.Amount.Value == 1 and Character.Humanoid.WalkToPoint == walkpart.Position then
						local gui = player.PlayerGui.Task:FindFirstChild("Mail")
						Character.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
						Character.Humanoid.WalkToPart = nil
						Character.HumanoidRootPart.Anchored = false
						if gui then
							local data = DataManager:Get(player)
							if player.PlayerGui.Task.Tasks.Amount.Value == 1 then
								if data then
									if player then
										game.ReplicatedStorage.sound:FireClient(player)
										data.cash+=50
										data.exp += 10
									end
								end

								local rand = math.random(1,5)
								if rand == 1 then
									player.PlayerGui.Task.Tasks.Amount.Value = 2
									gui.Adornee = workspace.Mail.mailpart2
									game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
								end
								if rand == 2 then
									player.PlayerGui.Task.Tasks.Amount.Value = 3
									gui.Adornee = workspace.Mail.mailpart3
									game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
								end
								if rand == 3 then
									player.PlayerGui.Task.Tasks.Amount.Value = 4
									gui.Adornee = workspace.Mail.mailpart4
									game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
								end
								if rand == 4 then
									player.PlayerGui.Task.Tasks.Amount.Value = 5
									gui.Adornee = workspace.Mail.mailpart5
									game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
								end
								if rand == 5 then
									player.PlayerGui.Task.Tasks.Amount.Value = 6
									gui.Adornee = workspace.Mail.mailpart6
									game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
								end

							end
							
						end
					end
					
					wait(5)
					cooldown2 = false
					cooldown = false
											
				end)
			end


			wait(8)
			if connection then
				Character.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
				connection:Disconnect()
				connection = nil
			end
		end)
	end

end)
1 Like