NPCs not following fast enough

I have a slight problem in which my Clones (or NPCs) take significant time to start pathfinding and walking, and I have no idea why.

This is my code snippet:

while Character do
			if #Path <= 0 then
				task.wait()
				continue
			end
			local Position1 = Vector3.new(Character:GetPivot().Position.X, 0, Character:GetPivot().Position.Z)
			local Position2 = Vector3.new(Path[1].X, 0, Path[1].Z)
			if (Position1 - Position2).Magnitude > 2 then
				table.insert(Path, 1, Character:GetPivot().Position)
				if #Path > (Amount+1) then
					table.remove(Path, #Path)
				end
				task.spawn(function()
					for Index, Clone in pairs(Clones) do
						if Path[Index] then
							Clone.Humanoid.WalkToPoint = Path[Index]
						end
					end
				end)
			end
			task.wait()
		end

While the characters are supposed to be right behind me, they are around 8 studs far.

I’ve came to realise that they (for some reason) take around 3-4 seconds standing before following me in the time that I start walking. But they don’t stop in the middle, so it’s unlike the hours of research I’ve tried on the devforums.

I have no idea what went wrong here and I need an explanation on what atleast happened.

There’s a lot of things that could be going wrong, but assuming your pathfinding function is working correctly, the first thing you want to do is check if a raycast can be shot between where they want to go and where they are (minus themselves, players, and other clones). If it can be, they can walk directly to the waypoint. I mean, it works most of the time but you’ll need a solution for elevation differences or pits.

Also, I’m pretty sure the closest waypoint to the target is the last one in the list.

The green targets are supposed to appear when I start walking, but they don’t so it partially isn’t the pathfinding of the NPC’s themselves, but the script is a little too slow to detect walking. Still no idea what to do.

You know what’s strange is that this shouldn’t really be working at all.

To start off, table.insert() can only ever insert an object at the end of a table, so you’re inserting the value “1” at the end instead of the position at the end. Switch it to table.insert(path, Character.Position)

Second, you’re always looking at the first entry when declaring Position2, so it’s pretty much going to always return true. Use the last value of Path instead.

Lastly, what defines the first value of Path? The loop skips over if Path is empty, but doesn’t declare an initial path value while it’s there, meaning if path is empty no movement is ever done.

I’ve only sent a code sample, so most questions here can be answered by sending the full code.

However, more importantly—I found out the issue.

Turns out, it was better practice to give Network Ownership to the Player controlling the NPCs and moving them within the Player’s LocalScript so that any delay is removed. It’s not the fault of my script.

Thank you for trying to help either way, I was able to dig the documentation and find out soon enough.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.