I’m working on NPCs that can pathfind, and having trouble with them becoming stutter-y every now and again. I’ve identified the issue is that the network owner is changing to the player, despite the fact I’m calling SetNetworkOwner(nil) on all the baseparts within the NPC character.
My code for setting the network owner initially: (I have confirmed this is being correctly set on all the parts of the character)
for _, part in ipairs(self.npcModel:GetDescendants()) do
if part:IsA("BasePart") then
part:SetNetworkOwner(nil)
part.CollisionGroup = npcCollisionGroup
end
end
You only need to call it on the root part, it’s not able to automatically be set to the player after doing so with script, so it probably isn’t the issue (?)
The network owner is 100% being set to the player, as shown by the network owner overlay (and confirmed by calling GetNetworkOwner()
I’ve tried calling SetNetworkOwner(nil) on just the HumanoidRootPart and all baseparts, but both result in the same. It seems like for a short while upon starting the game the network owner won’t get set, but eventually will and will then stay as the player.
I probably could continuously call SetNetworkOwner(nil) but seems counterproductive considering it should only need to be set once…
yes, npcFolder is under the workspace. Although, makes me wonder, could it be that I’m doing this all in the same frame at the very start of the game? Edit: Tried deferring the function call, didn’t make a difference.
self.npcModel = npcModel:Clone()
self.npcModel.Parent = npcFolder
self.npcModel:FindFirstChild("HumanoidRootPart"):SetNetworkOwner(nil)
for _, part in ipairs(self.npcModel:GetDescendants()) do
if part:IsA("BasePart") then
part:SetNetworkOwner(nil)
part.CollisionGroup = npcCollisionGroup
end
end
Could you send a barebones testplace to help debug the issue?
I’ve had similar problems with my NPCs where their networkowner could be auto-set again as soon as they equipped a tool (basically as any new part would be added to their assembly), but nothing indicates you’re doing that in your game.
I am sitting the NPC down on a Seat object for a bit, and only after does the player randomly become the network owner, and from testing it seems that might actually be the cause. Does setting a character on a seat cause changes to network ownership?
Edit: If so, is the solution to call SetNetworkOwner(nil) every time they get up from the seat as well?
The character gets welded to the seat, and therefore becomes a part of that seat’s assembly. If the seat is connected to anchored parts, the character no longer has a networkOwner, and once they get unwelded, I think their ownership would be set to auto again. If the seat’s assembly is not anchored, you could try setting the ownership to nil again so the character can control whatever’s connected to the seat. But in short, yeah, calling SetNetworkOwner(nil) each time the character jumps off of the seat would be a good solution.