The code below is what I was trying to do but it wont work, on line 100 (Marked with a comment) it prints v.Pet but on line 102 (Also marked with a comment) it says Pet is not a valid member of “Parent”. Does anyone have a solution or any suggestions to maybe fix this?
script.Parent.InventoryFrame.UnequipAllButton.MouseButton1Click:Connect(function()
for i, v in pairs(script.Parent.InventoryFrame.PetsHolder:GetChildren()) do
if v:IsA("ImageButton") then
print(v.Pet.Value) -- Line 100
if v.Pet.Value.Parent == player.EquippedPets then -- Line 102
local petToMove = v.Pet.Value
petToMove.Parent = player.Pets
v.ImageColor3 = Color3.new(1, 1, 1)
refreshInventory()
end
end
end
end)
Check if the objectvalue has an existing value AND if that value has a current ancestor too. When an instance is destroyed, the ObjectValue doesn’t change its value to nil (or at least not instantly), and you have to do either a check when you plan to use that valueholder (which is what i do) or set an event that checks when the instance was deleted and sets the objectvalue’s value to nil.
Do some check like this before the ‘line 100’:
if v.Pet.Value and (player.EquippedPets:IsAncestorOf(v.Pet.Value) or player.Pets:IsAncestorOf(v.Pet.Value)) then
–code
end
If your function is supposed to ignore deleted pets and run the code only on existing ones, I guess this problem would be solved with the code I mentioned. But if it is supposed to always find an ancestor and it isn’t doing so, there is probably something deleting the pet on the experience, and you have to find out what it is.