Issues with SetNetworkOwner() API

Well, I just wanted to make a block, or a pet, if u wanna call it that, that follows you.

This is what I implemented:

  1. When player’s added, make a folder in workspace labeled Player’s Pets, then connect an event that setnetwork owner on parts parented to this folder
  2. I stored the part in server storage, then, via a remote, it sets this part’s parent to the folder in workspace
  3. After that I control the part from the client.

here’s the code I wrote if it helps

local function OnPetEquip(Player, Command)
	local PetsFolder = workspace:FindFirstChild(Player.Name.."'s Pets")
	if PetsFolder and Command then
		local PetAssign = TempPetTemplate:Clone()
		PetAssign.Name = "Pet"
		PetAssign.Parent = PetsFolder
		return PetAssign
	elseif PetsFolder and not Command then
		local Pet = PetsFolder:FindFirstChild("Pet")
		if Pet then
			Pet:Destroy()
		end
	end
	
	warn(Player.Name..".Pets directory not found in the player object.")
	return nil
end


local function OnPlayerAdded(Player)
	Player.CharacterAdded:Wait()
	local Pets = Instance.new("Folder")
	Pets.Name = Player.Name.."'s Pets"
	Pets.Parent = workspace
	
	Pets.ChildAdded:Connect(function(Child)
		if Child:IsA("BasePart") then
			Child:SetNetworkOwner(Player) --error points to this line
		end
	end)
end

game.Players.PlayerAdded:Connect(OnPlayerAdded)
ReplicatedStorage.PetEquip.OnServerInvoke = OnPetEquip

it works as expected, yet I keep getting the error. The error is what’s bothering me as it’s appearing on the line commented above.

Network Ownership API cannot be called on Anchored parts or parts welded to Anchored parts.

Yet the part is NOT anchored :man_shrugging:, it’s also a single part, so it’s not welded. I mean, isn’t is supposed to be error-free.

Hi. You said the part was anchored didnt you. Wouldnt that be the issue?

Oops

I meant the part is NOT anchored, but still gives me an error. Thanks for the correction.
jeez, i really need sleep, i made the mistake twice

If the part is anchored, then the error comes up because you cant set the network ownership of an anchored part. This is because when you’re giving ownership, you’re allowing the client to control the physics of the part, but there are no physics to an anchored part.

This is from memory, so I’ll have to double check.

1 Like

As I said, the script works as expected.

Yet, the error persists. (edited last post)

Add an if statement to check the part isnt anchored. If it is, print the part name. If no name appears, then you’ve solved half of your problem. (By half your problem, I mean that you know you’re not accidentally transferring ownership to an anchored part)

I’ve read through your code multiple times and have no idea.

I tried these 2 before posting this

  1. I checked if the part is anchored, but it really just errored and didn’t function at all (weird, since it was not anchored in the first place)
  2. I checked if CanSetNetworkOwner(), did the same thing.

However, I found a fix, I set the network owner just as I was equipping the pet. Guess what I learn is never set network owner in ChildAdded events :man_shrugging:

Here’s what I did

local function OnPetEquipEvent(Player, Command)
	if Command then
		local PetAssign = TempPetTemplate:Clone()
		PetAssign.Name = "Pet"
		PetAssign.Parent = workspace
		PetAssign:SetNetworkOwner(Player)
		return PetAssign
	elseif Command == false then
		local Pet = workspace:FindFirstChild("Pet")
		if Pet then
			Pet:Destroy()
		end
	end
	
	warn(Player.Name..".Pets directory not found in the player object.")
	return nil
end

ReplicatedStorage.PetEquip.OnServerInvoke = OnPetEquipEvent
1 Like