Whenever a player comes close to another player's pet, the pet "launches" away

Hello everyone,

I am making pets for a game but I’m running into a problem I can’t seem to fix.

I managed to make the pet fly and follow a player but whenever another player comes close to another player’s pet, the pet launches away

I use BodyGyro, BodyPosition and a localscript for this. The pet is already set to non-collidable.
image
(Coiny is the name of the pet)

This is the code of the localscript:


local char = script.Parent.Parent
local hum = char:FindFirstChild("Humanoid")
local torso = char:FindFirstChild("UpperTorso")
local pet = script.Parent

local maxFloat = 1
local floatInc = 0.025
local sw = false
local fl = 0

while true do
	wait()
	if not sw then
		fl = fl + floatInc
		if fl >= maxFloat then
			sw = true
		end
	else
		fl = fl - floatInc
		if fl <=-maxFloat then
			sw = false
		end
	end
	if pet ~= nil and hum ~= nil and torso ~= nil then
		if hum.Health >= 0 then
			local cf = torso.CFrame * CFrame.new(3,2+fl,3)
			pet.BodyPosition.Position = Vector3.new(cf.x,cf.y,cf.z)
			pet.BodyGyro.CFrame = torso.CFrame * CFrame.new(3,0,-3)
		else
			break
		end
	end
end

Does someone know a solution to this problem?

Did you set the NetworkOwnership of the pet to the pet’s owner?

pet:SetNetworkOwner(Player)

That worked! thank you so much.

I Never used SetNetworkOwner so I didn’t know that was the solution.

When a player equips a pet, the pet will get duplicated from serverstorage and is put in the player’s character. I’ve made it so whenever a player equips a pet, the server does pet:SetNetworkOwner(Player)

Thank you a lot!

To explain further what Network Ownership does, it prevents other players from claiming physics ownership of that object. If this is unset, the default behaviour will be whoever the closest player is will claim ownership of that object. If this is set to specific player, like the pet’s owner, that player will claim ownership of that object so that other players can’t mess with its velocity, rotation velocity etc.

Network ownership also allows the owning client to replicate the physics of that object to other clients, which in many cases can be powerful and helpful depending on the context of it’s usage.

It’s sad that clients are not allowed to change ownership settings in a LocalScript
But you can change settings in a server script

local players = (game:FindService("Players") or game:GetService("Players"))
local char = script.Parent.Parent
local player = players:GetPlayerFromCharacter(char)

wait()
script.Parent:SetNetworkOwner(player)

Why is that sad? Allowing a client to be authoritative of network ownership would lead to a plethora of issues not only in terms of exploitation but developers forming extremely bad habits. Clients controlling network ownership is also counterintuitive to the paradigm of leaving important system control to the server.

Also, you can’t set the network owner of a Player. Network ownership API is reserved to BaseParts, since network ownership pertains to physically simulated parts and furthermore the physics engine. What you probably meant to do was set the network ownership of the BasePart descendants of the character, though there’s no need to do that because it is done by the backend when the character is being added.