Hello, I am trying to set a pet’s network ownership to the player that owns it, but I get this error.
Can only call Network Ownership API on a part that is descendent of workspace
I understand what this means, but the pet is parented to the character, so I don’t know what could be wrong.
Here is my code
function givePetEmojiCool(player, char)
if player then
local character = char
if character then
print("adding pet emojicool")
local humRootPart = character.HumanoidRootPart
local newPet = CoolEmojiPet:Clone()
newPet.Name = "CoolEmojiTemplate"
newPet.Parent = game.Workspace
local head = newPet.HeadEmoji
head.CFrame = humRootPart.CFrame * CFrame.new(10, 10, 10)
local heliCopter = newPet.HeliCopter
head.Anchored = false
newPet.Parent = character
head:SetNetworkOwner(player)
local bodyPos = Instance.new("BodyPosition", head)
bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyPos.P = 10000
local bodyGyro = Instance.new("BodyGyro", head)
bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
petList[player] = "EmojiCool"
local rv = bodyGyro.CFrame.rightVector
local stepped
local humanoid = character.Humanoid
stepped = runService.Stepped:Connect(function()
heliCopter.Orientation = Vector3.new(0, heliCopter.Orientation.Y - 2.5, 0)
bodyPos.Position = humRootPart.Position + Vector3.new(2, 3, -6)
bodyGyro.CFrame = humRootPart.CFrame * CFrame.Angles(0, math.rad(90), 0)
end)
humanoid.Died:Connect(function()
stepped:Disconnect()
local char = player.CharacterAdded:Wait()
local player = game.Players:GetPlayerFromCharacter(char)
givePetEmojiCool(player, char)
end)
end
end
end
I’m not really sure how to solve this, you can try parenting the pet to workspace instead of character and see if that works.
The next conditional:
character.Parent ~= workspace
It might not be working since the code is running faster than the character creation, so you need to delay it a little either by waiting for the CharacterAdded event or doing a simple wait.
The first time it runs, it prints workspace. I run the function the first time, when the player touches a gui that activates the pet, which is why it works the first time, but when the player dies, it prints nil.
I added the CharacterAdded:Wait() in the original code, but it still does the same thing. I already tested putting the pet in workspace, and it works, but I would rather not since in parts of my code, I am relying on the pet being inside of the character. But, if that’s the best solution then I’ll do try it.
Is the error occuring because the character is not yet in the workspace?