:MoveTo() delaying in a clone

After cloning the dummy, he’s detect my character but don’t move to him, I put a print in this part of script:

enemy.Humanoid:MoveTo(target.PrimaryPart.Position)
	print(target)
	enemy.Moving.Value = true
	enemy.Humanoid.MoveToFinished:Connect(function()
	enemy.Moving.Value = false
end)

The target is the player detected in the dummy zone, the dummy follow him when is detected. He really detects the target (Me) but don’t move with MoveTo(), and yes, he is not anchored or have WalkSpeed = 0.

What exactly are you tryjng to do here?

Are you trying to move the character/dummy to the players character when in range?

Yes, is this I’m trying, but he have a delay after this. When he spawns he have an animation, when the animation finish, the HumanoidRootPart stops to be anchored and in theory he can move to the player.

Why are you using MoveTo() on the model rather than tweening the humanoid root part?

Because the dummy need to walk to player.

Can you provide a video to show what happens?

The mummy has a delay when spawned.

There isn’t enough information provided to accurately determine why the enemy is getting stuck

If you’re calling MoveTo only once, the enemy will move to the player’s position at the time that it was called, and stop there, unless you’re tracking the player’s position within a loop and instructing the enemy to follow the player’s new position

Are you using PathfindingService to calculate the enemy’s path?

No, I’m using a loop, don’t make sense have a delay when I using a loop, I check with prints and in theory, the :MoveTo() is executed.

It would be very helpful to show the whole script, if possible

I mean, it’s definitely possible to have delays within loops


@6Communist_Wolf9

You can use this script for your enemy, if you want:

-- Tested using a server script that's a direct child of the enemy NPC
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")

local DETECT_RADIUS = 32 -- Farthest distance that the enemy will be able to detect players. In studs

local enemy = script.Parent
local enemyRootPart = enemy.PrimaryPart or enemy:WaitForChild("HumanoidRootPart")
local humanoid = enemy:WaitForChild("Humanoid"):: Humanoid

local path = PathfindingService:CreatePath({
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = false}) -- Jumping support will require modifying the code a bit


local function getClosestPlayer(): (Player?, Vector3?)
	local closestPlayer, playerPosition = nil, nil
	local distanceFromPlayer = math.huge

	for _, player in Players:GetPlayers() do
		local safe = player:FindFirstChild("Safe"):: BoolValue?
		if safe and safe.Value == true then continue end -- This will skip this player if they're marked as safe

		local character = player.Character
		if character == nil then continue end -- This will skip the player if they don't have a character

		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid and humanoid.Health <= 0 then continue end -- This will skip the player if they're already dead

		local rootPart = character.PrimaryPart
		local distance = rootPart and (rootPart.Position - enemyRootPart.Position).Magnitude

		if distance and distance <= DETECT_RADIUS and distance < distanceFromPlayer then
			closestPlayer = player
			playerPosition = rootPart.Position
			distanceFromPlayer = distance
		end
	end

	return closestPlayer, playerPosition
end

enemyRootPart:SetNetworkOwner(nil) -- This will prevent players from being able to teleport the enemy...
-- ...if they automatically become the network owner

while true do
	local closestPlayer, playerPosition = getClosestPlayer()

	if closestPlayer then
		path:ComputeAsync(enemyRootPart.Position, playerPosition)

		if path.Status == Enum.PathStatus.Success then
			for _, waypoint in path:GetWaypoints() do
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	end

	task.wait()
end -- For future reference, code written below this line won't run, as it will be blocked by the loop

I tested it using a server script that’s parented to one of the rigs generated by the rig builder tool


@6Communist_Wolf9

I’ve made some changes to the script above, so that it will skip players that are marked as safe, or are already dead. It should work the same, or at least very close, to the script you’re currently using :slight_smile::+1:

Original version of my script, before adding the changes mentioned above
-- Tested using a server script that's a direct child of the enemy NPC
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")

local DETECT_RADIUS = 32 -- Farthest distance that the enemy will be able to detect players. In studs

local enemy = script.Parent
local enemyRootPart = enemy.PrimaryPart or enemy:WaitForChild("HumanoidRootPart")
local humanoid = enemy:WaitForChild("Humanoid"):: Humanoid

local path = PathfindingService:CreatePath({
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = false}) -- Jumping support will require modifying the code a bit


local function getClosestPlayer(): (Player?, Vector3?)
	local closestPlayer, playerPosition = nil, nil
	local distanceFromPlayer = math.huge

	for _, player in Players:GetPlayers() do
		local rootPart = player.Character and player.Character.PrimaryPart
		local distance = rootPart and (rootPart.Position - enemyRootPart.Position).Magnitude

		if distance and distance <= DETECT_RADIUS and distance < distanceFromPlayer then
			closestPlayer = player
			playerPosition = rootPart.Position
			distanceFromPlayer = distance
		end
	end

	return closestPlayer, playerPosition
end

enemyRootPart:SetNetworkOwner(nil) -- This will prevent players from being able to teleport the enemy...
-- ...if they automatically become the network owner

while true do
	local closestPlayer, playerPosition = getClosestPlayer()

	if closestPlayer then
		path:ComputeAsync(enemyRootPart.Position, playerPosition)

		if path.Status == Enum.PathStatus.Success then
			for _, waypoint in path:GetWaypoints() do
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	end

	task.wait()
end -- For future reference, code written below this line won't run, as it will be blocked by the loop
while wait(0.1) do
		if not enemy:FindFirstChild('Humanoid') then
			break
		end
		if enemy.PrimaryPart:FindFirstChild('Zone') then
			for _, s in pairs(workspace:GetPartsInPart(enemy.PrimaryPart:WaitForChild('Zone'), OP)) do
				if s.Parent:FindFirstChild("Humanoid") then
					if game.Players:GetPlayerFromCharacter(s.Parent) then
						if game.Players:GetPlayerFromCharacter(s.Parent).Safe.Value == false and s.Parent.Humanoid.Health > 0 then
							detect(s.Parent)
						end
					end
				end
			end
			if state == false then
				QuickDistance = math.huge
				target = nil
			end
			state = false
			if enemy:FindFirstChild('Moving') then
				if target ~= nil and enemy.PrimaryPart:FindFirstChild('Hitbox') then
					enemy.Humanoid:MoveTo(target.PrimaryPart.Position)
					enemy.Moving.Value = true
					enemy.Humanoid.MoveToFinished:Connect(function()
						enemy.Moving.Value = false
					end)
				elseif (enemy.PrimaryPart.Position - pos).Magnitude > 0.05 then
					enemy.Humanoid:MoveTo(pos)
					enemy.Moving.Value = true
					enemy.Humanoid.MoveToFinished:Connect(function()
						enemy.Moving.Value = false
					end)
				end
			end
			target = nil
			QuickDistance = math.huge
		end
	end
local function detect(char)
		local calc = (enemy.PrimaryPart.Position - char.PrimaryPart.Position).Magnitude
		state = true
		if calc < QuickDistance then
			QuickDistance = calc
			target = char
		end
	end

I don’t have a solution for this for now, anyone can help?