In my game when the player leaves, I save a JSONEncoded table of a player’s pets that are currently equipped. When they join, I JSONDecode the string and equip the pets according to what was saved. This works fine in studio, however, when I play it in the actual game, it errors whenever I join. Here is the code:
local function addPetNecessities(clonedPet, player)
local character = player.Character
-- Give pet necessities
for _, child in pairs(script.PetChildren:GetChildren()) do
local clonedChild = child:Clone()
if clonedChild:IsA("LocalScript") then
clonedChild.Disabled = false
end
clonedChild.Parent = clonedPet.PrimaryPart
end
-- Add pet to character
clonedPet.Parent = character:WaitForChild("Pets")
-- Set network owner to player
for _, part in pairs(clonedPet:GetDescendants()) do
if part:IsA("BasePart") then
part:SetNetworkOwner(player) -- Error line
end
end
end
Here is the error I get whenever I join. The function is called with player.CharacterAppearanceLoaded()
to ensure the player’s character has loaded, and I use a :WaitForChild()
to make sure the pet folder is actually there in workspace.
I am confused at this because I set the pet’s parent to a descendant in workspace, however the error says it is not in workspace.
I have tried many things and still am having trouble to figure out what the issue is. Any help is appreciated, thank you!
FYI:
- I am using DataStore2 to save the JSONEncoded table in a string.
- The function is in a ModuleScript and is called in a ServerScript when
player.CharacterAppearanceLoaded()
is called. - Yes, the game is published.