Strange issue with moving NPCs

So I’ve almost finished fully implementing this pathfinding algorithm in my game. The problem I’m having right now is that after around a minute of testing client-side any pathfinding NPC within a certain distance of my character will almost entirely cease to move. This only happens after some time. Also, my “area of effect” on the NPCs seems to increase in size as time goes on. To elaborate, 1 minute in I have to get closer to an NPC for them to experience this problem in contrast to around 2 where I don’t have to be as close to see the same effect. They will eventually move but only to the next node and only after a pretty long amount of time. I’ve run the game server-side only and it has absolutely no issues. In client-side testing I also checked to see if the server was seeing the same thing as me so I switched to server view and the NPCs were also in the same “frozen” state. To make it absolutely clear: only NPCs within a certain distance of me will have trouble moving. Anything far away has no problem. Even with only 1 NPC this issue will arise. I also destroyed the NPC and spawned a new one to see if the problem carried over and sure enough it did. Anyway, when the NPCs pathfind, they itterate through a table that includes all of the nodes to a finish node (the path). They use Humanoid:MoveTo(node_position) to move to the next node and will only move to the next node after Humanoid.MoveToFinished:Wait(). It seems that Humanoid.MoveToFinished:Wait() is either part of the problem or is at least the one succumbing to the problem based on some print tests. Humanoid.MoveToFinished:Wait() is the part that gets hung up more than it should. Absolutely no lag or errors. Here’s the issue visually:

stutter

This issue is absolutely beyond me at this point. I’ve been looking everywhere for something that addresses my problem, but have found nothing direct. I’ve seen that there are a lot of issues to do with Humanoids and things related though so I’m just praying that the issue is within my control. I will be happy to share code, I just wasn’t sure what parts would be relevant enough to paste into a block for. Any advice/help is much appreciated!

2 Likes

This is really strange and I don’t know the exact details but it has to be something to do with network ownership. As a player spawns their physics network ownership bubble slowly increases, meaning if a physics object is within a spherical volume around the player then the player ‘owns’ the object and the player’s computer will perform the physics calculations of that object and send the CFrame info of the object back to the server. For all the physics objects outside of the bubble and outside of other players’ bubbles, the server does the physics calculations and send the CFrame info of the object to the players. If you want to see the growth of this bubble go to ‘Studio Settings’ –> ‘Physics’ –> ‘Are Regions Shown’ and enable it. You can also enable ‘Are Owners Shown’ to see which physics objects your computer ‘owns’. The bubble will stop increasing/shrink if it ‘owns’ too many objects.
From server scripts you can control network ownership using GetNetworkOwner() and SetNetworkOwner(). Some good examples found here. Most people generally do HumanoidRootPart:SetNetworkOwner(nil) – which means the server will always have ownership no matter what – for all NPC humanoids because there is usually a visible and annoying stutter of humanoid movement as it crosses between server and player network ownership. In your case, I have no idea why it would just stop dead in its tracks, maybe because it’s moving so fast. I’d be interested if the problem persists even with a much slower movement speed.

2 Likes

This makes a lot of sense and I’m going to try some tests/solutions right now

1 Like

So first I tried to slow down the WalkSpeed of the NPC and the same problem arose. As you can see the issue only shows up when the NPC is within my network bubble, so this was in fact the issue.

areaofeffect

I included :SetNetworkOwner() when the NPC was being spawned and now it’s working flawlessly!
I just used:

newEntity.HumanoidRootPart:SetNetworkOwner(nil)

Thanks so much.

2 Likes