In my game, when a player has pets equipped, they save and load when the player rejoins the game.
However, sometimes that works, sometimes it does not. I have not figured out what’s different when it does not work.
For each pet a player owns, a folder named after the pet is created under Players[LocalPlayer].Pets
Each pet folder has a bool value for ‘equipped’.
The folders load, the bool value for each pet equipped is displayed correctly, the UI displays owned pets and equipped pets correctly as well, but there is no pet to be seen sometimes. When I then try to unequip the non-visible pet, it naturally gives me an error.
Here is the script that’s supposed to equip the pets when a player joins:
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
local Pets = plr:WaitForChild("Pets")
local Children = Pets:GetChildren()
local PetsEquipped = {}
for i = 1,#Children do
if Children[i].Equipped.Value == true then
PetsEquipped[#PetsEquipped+1] = Children[i].Pet_ID.Value
end
end
local Offset = 360/(#PetsEquipped)
local Character = plr.Character
local CircleRadius = 7.5
Character:GetPropertyChangedSignal("Parent"):Connect(function()
if Character.Parent == workspace then
for i = 1,#PetsEquipped do
local PetFolder = getFolder(PetsEquipped[i],plr)
local Pet = game.ReplicatedStorage.PetLibrary:FindFirstChild(PetFolder.Name):Clone()
Pet.Pos.Value = GetPointOnCircle(CircleRadius, Offset*i)
Pet.Pet_ID.Value = PetsEquipped[i]
Pet.Parent = Character
Pet.Body:SetNetworkOwner(plr)
end
end
end)
end)
end)
I think it might be because you’re waiting for the parent of the Character to change AFTER the character has been added (which by default is to the workspace.)
I’ve just tried the following but that didn’t work:
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
local Pets = plr:WaitForChild("Pets")
local Children = Pets:GetChildren()
local PetsEquipped = {}
for i = 1,#Children do
if Children[i].Equipped.Value == true then
PetsEquipped[#PetsEquipped+1] = Children[i].Pet_ID.Value
end
end
local Offset = 360/(#PetsEquipped)
local Character = plr.Character
local CircleRadius = 7.5
for i = 1,#PetsEquipped do
local PetFolder = getFolder(PetsEquipped[i],plr)
local Pet = game.ReplicatedStorage.PetLibrary:FindFirstChild(PetFolder.Name):Clone()
Pet.Settings.Pos.Value = GetPointOnCircle(CircleRadius, Offset*i)
Pet.Pet_ID.Value = PetsEquipped[i]
Pet.Parent = Character
Pet.Body:SetNetworkOwner(plr)
end
end)
When player spawns server creates a default R15 character and then when the Player:HasAppearanceLoaded() becomes true the server immediately destroys that character and creates a character that matches Player.CharacterAppearanceId. That means you shouldn’t use plr.CharacterAdded (because it fires when default character spawns) but plr.CharacterAppearanceLoaded so it fires once Player:HasAppearanceLoaded() == true.