Pathfinding AI twitching while moving

My AI I am creating uses pathfinding to get to an enemy for a game I am creating.

For some reason it starts walking behind it for a moment then it turns back moving towards the enemy.

I tried making it move for a specific waypoint every heartbeat instead of following a for loop which fixes it but then the AI stands still in the middle of moving for a moment.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Here is the function I am using to move the AI which gets called every heartbeat:

local goTo = function(position)
	local path = pathfindingService:CreatePath()
	path:ComputeAsync(script.Parent.PrimaryPart.Position, position)
	if path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		for i, v in pairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				script.Parent.Humanoid.Jump = true
			end
			script.Parent.Humanoid:MoveTo(v.Position)
			script.Parent.Humanoid.MoveToFinished:Wait()
		end
	else
		script.Parent.Humanoid:MoveTo(position)
	end
end

Thanks in advance.

use SetNetworkOwner

for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("BasePart") then
v:SetNetworkOwner(nil)
end
end

or you could try setting the HRP’s network owner to nil

1 Like

I have that in an unmentioned part at the bottom, but i will try doing all the baseparts next time im available, however I feel this is not the fix.

well can you atleast send a video of the “twitching” so i can see it

Will do when i can, I live in the uk so its pretty late for right now.

oh alr that makes since (is it like 1am for you guys)
and really quick although this may not be the solution; if your trying to run 2 moveto() at the same time (one for player, one for waypoint) thats also what could cause it

(and try setting the network owner in the same script)

I have placed that inside the same script, thats what i thought when i first encounted the issue, i believe that irs because 2 loops are occuring and telling it to move there when the other loop is telling it elsewhere, and yes its 1am lol

if you are running 2 MoveTo() functions at the same time, then im pretty sure thats the problem; try to run 1 MoveTo() IF a player isnt near, ELSE (if a player is near) run the MoveTo() for the player

I need to find a way to make the loop stop when another starts then right? I think I may have an idea.

my idea was to use a if else like i said here

example;

--loop
if foundPlayer == false then
--do normal walk pathfinding
else
--do pathfinding for player
end
--loop

Unfortunately this did not work and nor did my solution.

I created a method where I do what I mentioned I did in the description but in a way without an extra feature which I did not need anymore and fixed it. Thanks for helping :slight_smile:

1 Like