SetNetworkOwnership(nil) does not work, tried every way possibe

Hello! So I have been fighting with pathfinding for a solid amount of time and found out that ‘SetNetworkOwnership’ could perhaps fix the issue, but I guess not for me? I have tried these ways so far:

NPCmodel:SetNetworkOwnership(nil)

NPCmodel.PrimaryPart:SetNetworkOwnership(nil)

for _,basepart in pairs(NPCmodel:GetChildren()) do
       if basepart:IsA("BasePart") then
         pcall(function()
             basepart:SetNetworkOwner(nil)
        end)
    end
end

nothing works, and no more different tutorials, help please? I am trying to make pathfinding for it to not be laggy/stutter.

You cannot set the network owner of a Model. Instead, you will have to go through every descendant that is a BasePart.

I wrote a module for this that you can use:

local function setNetworkOwnerOfBasePart(basePart, networkOwner)
	local success, errorReason = basePart:CanSetNetworkOwnership()
	if success then
		basePart:SetNetworkOwner(networkOwner)
	else
		-- Sometimes this can fail, so throw an error to prevent
		-- ... mixed networkownership in the 'model'
		error(errorReason)
	end
end

local function setNetworkOwnerOfModel(model, networkOwner)
	for _, descendant in pairs(model:GetDescendants()) do
		-- Go through each part of the model
		if descendant:IsA("BasePart") then
			-- Try to set the network owner
			setNetworkOwnerOfBasePart(descendant, networkOwner)
		end
	end
end

local function setNetworkOwner(instance, networkOwner)
	if instance:IsA("Model") then
		setNetworkOwnerOfModel(instance, networkOwner)
	elseif instance:IsA("BasePart") then
		setNetworkOwnerOfBasePart(instance, networkOwner)
	else
		warn(instance.Name .. "'s network ownership cannot be modified.")
	end
end

return setNetworkOwner

This module throws an error if it fails to set the network owner.

Setting the network owner of every child will not work for NPCs that have hats, since a hat inside a character can also contain a BasePart.

Oh! Thank you! I tried your method before, but it does not work sadly, it always gives me this error:

Put my code in a ModuleScript and load it in your script. Then you can code setNetworkOwner(myModel, nil).

local setNetworkOwner = require(path.to.that.module)

setNetworkOwner(yourNPC, nil)
1 Like

Okay, I will try it, I will contact you later on if it succeeded or not.

Sadly, it did not work. Still gives me the error.

That error just means that the npc model is somehow connected to an anchored part or is anchored in itself. Just add a print function before setting the network owner so you can see which part is erroring in case whatever is anchored isn’t obvious

1 Like

if partnameorwhatever.Anchored == false then

2 Likes

Thank you, I will check it out and try to fix it!!

I have checked it. Every basepart in NPC model errored, none of them are anchored nor welded to another part. Do you have an idea on how it could be fixed perhaps?

I still need help! If anyone is able, then please!

His code has a typo. Instead of SetNetworkOwnerShip(), it needs to be SetNetworkOwner() sorry for the bump.

2 Likes