How to make my equipping system work?

I want to make my Pet Equipping System work

When I try to get a pet it goes in my inventory and when I equip it, it keeps the equip image from going back to unequipped and wont create a new pet.
When I first equip it and open the inventory:
image

When I close and re-open the inventory (NOTE: This also creates a new pet and wont destroy it on unequip):
image

I have tried devforum and searching up on google but it doesn’t seem to find the answer I am trying to find. Here is my scripts for the Equipping and the updateFrame function so it doesn’t duplicate the pet when the player re-opens the inventory.

NOTE: This is all Hard Coded I made it myself because I am trying to do this for the first time without YouTube tutorials

--// EquipScript, LocalScript \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local petMod = require(ReplicatedStorage:WaitForChild("PetModule"))
local player = game:GetService("Players").LocalPlayer
local realPet
local debounce = false
local equipped = script.Parent:WaitForChild("Equipped").Value


local equippingSettings = {
	Settings = {
		Equip = {
			BackgroundColor3 = Color3.new(0,255,0),
			Image = "rbxassetid://247421287"
		},
		Unequip = {
			BackgroundColor3 = Color3.new(255,0,0),
			Image = "rbxassetid://149260272"
		}
	}
}

script.Parent.EquipButton.Activated:Connect(function()
	local petName_ = script.Parent:WaitForChild("PetName").Text
	local pet = game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild(petName_):Clone()
	if not debounce then
		debounce = true
		if not equipped then
			realPet = petMod.setPetMovementSettings("", pet.Name, player)
			equipped = true
			script.Parent.EquipButton.Image = equippingSettings.Settings.Equip.Image
			script.Parent.EquipButton.BackgroundColor3 = equippingSettings.Settings.Equip.BackgroundColor3
		else
			realPet:Destroy()
			equipped = false
			script.Parent.EquipButton.Image = equippingSettings.Settings.Unequip.Image
			script.Parent.EquipButton.BackgroundColor3 = equippingSettings.Settings.Unequip.BackgroundColor3
		end
		wait(0.1)
		debounce = false
	end
end)
--// updateFrame, function, LocalScript \\--
function updateFrame()
	for index,value in pairs(script.Parent.MainFrame.PetHolder:GetChildren()) do
		if not value:IsA("UIGridLayout") then
			value:Destroy()
		end
	end
	for index, value in pairs(petMod.OwnedPets) do
		local cloneE = petIcon:Clone()
		cloneE.Parent = script.Parent:WaitForChild("MainFrame"):WaitForChild("PetHolder")
		cloneE.PetName.Text = value.Settings.PetName
		cloneE.PetRarity.Text = value.Settings.Rarity
		cloneE.PetImage.Image = "rbxassetid://8292772975"
		cloneE.PetImage.ImageColor3 = game.ReplicatedStorage:WaitForChild("Pets"):WaitForChild(value.Settings.PetName).Color
	end
end

I am trying to make the pet stay equipped and when I close and open the inventory I can unequip the pet and the image stays with the checkmark and doesn’t go back to an X, so when I unequip it, it will not create a new pet it will just destroy the old equipped pet. So I am technically trying to find a way to refresh all of the petIcons without them resetting all of the settings.

Sorry if I couldn’t explain this very well. I am not too good at explaining things.

Try making an array containing all the equiped pets and another array containing all the owned pets. Then you can just set the pet models parent to nil when unequpping and back to workspace (or wherever) when equipping it. You can probably do this is the PetModule by adding a table for the equipped pets.

P.S. - when creating a new instance you should set it’s parent last. It is explained in this post:

This isn’t what I’m looking for through with this Link on the Forum Post but I will try your message instructions later.

I know. It wasn’t meant to provide a tutorial or whatever, it was meant as a small nugget of information to help optimize your code because you currently define the clones parent argument before all it’s other values.