How could I fix this bug with MoveTo()?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to move a humanoid.

  2. What is the issue? Include screenshots / videos if possible!
    MoveTo() makes it so the enemy walks in place for about 1-2 seconds.

Here’s my code (noob is just the enemy model):

    local players = game.Players:GetChildren()
	local config = noob.config

	local rs = game:GetService("RunService")
	
	rs.Stepped:Connect(function()
		local shortestDistancePlr = {math.huge,"Nobody"}
		for i,v in pairs(players) do
			local plr = workspace:FindFirstChild(v.Name)
			if plr then
				local magn = (plr.HumanoidRootPart.Position - noob.HumanoidRootPart.Position).Magnitude
				if magn <= shortestDistancePlr[1] then
					shortestDistancePlr = {magn, v.Name}
				end
			end
		end
		if shortestDistancePlr[2] ~= "Nobody" and shortestDistancePlr[1] <= config.aggro.Value * 2 then 
			noob.Humanoid:MoveTo(workspace:FindFirstChild(shortestDistancePlr[2]).HumanoidRootPart.Position)
		end
	end)

I am a little confused.

If I am understanding correctly you could use :PivotTo() on the player model:

It looks like PivotTo() can only teleport, but I want it to move like an actual player to the location. I’m trying to make an enemy for a game, if that helps.

1 Like

On the enemies Humanoid, you could set the walkspeed to whatever you want and then use :MoveTo() maybe?

Like I said, MoveTo() seems to not work very well and sometimes doesn’t work at all.

Model:MoveTo or Humanoid:MoveTo?

I’m currently using Humanoid:MoveTo.

try putting print statements before and after :MoveTo and check if they both print…

MoveTo works, but not very well. I found out why it didn’t work sometimes but I think I’ll have to explain what I mean by MoveTo working weirdly.
Once you get in the activation range of the enemy, it walks in place for about 1-2 seconds, teleports where it was supposed to move to, and works normally from there.

Here’s my code:

local players = game.Players:GetChildren()
	local config = noob.config

	local rs = game:GetService("RunService")
	
	rs.Stepped:Connect(function()
		local shortestDistancePlr = {math.huge,"Nobody"}
		for i,v in pairs(players) do
			local plr = workspace:FindFirstChild(v.Name)
			if plr then
				local magn = (plr.HumanoidRootPart.Position - noob.HumanoidRootPart.Position).Magnitude
				if magn <= shortestDistancePlr[1] then
					shortestDistancePlr = {magn, v.Name}
				end
			end
		end
		if shortestDistancePlr[2] ~= "Nobody" and shortestDistancePlr[1] <= config.aggro.Value * 2 then 
			noob.Humanoid:MoveTo(workspace:FindFirstChild(shortestDistancePlr[2]).HumanoidRootPart.Position)
		end
	end)

Oh that’s weird

It’s possible that the teleporting is caused by the ownership of the NPC changing from the server to the player

To fix this simply call SetNetworkOwner() and provide nil as it’s argument this will prevent the NPC’s parts from changing ownership thus fixing the teleporting.

Keep in mind that SetNetworkOwner() is for BaseParts so you would need to iterate through every BasePart in the NPC’s model

task.wait() -- This is necessary for some reason

for _,Part in ipairs(noob:GetChildren()) do
      if Part:IsA("BasePart") and Part:CanSetNetworkOwnership() then
         Part:SetNetworkOwner(nil) 
end

1 Like

Thank you, this fixes the teleportation, but the enemy still walks in place for 1-2 seconds before chasing the player.

that most likely means that the NPC is trying to detect players every time regardless if it chases someone or not, I would recommend to make the NPC not check for players if it detects a player and the player is in it’s range

it’s supposed to be multiplayer though, i want it to switch to someone else if they get closer to the enemy than the player it’s currently chasing