How i can delete old crafted pets in my inventory?

I have a pet system, it works as it should, but there is one problem with crafting pets, when I craft 5 pets into one, new cooler ones appear and for some reason the old ones remain in place and are not deleted :frowning:

how to fix this?

local RS = game.ReplicatedStorage
local Inventory = script.Parent.Parent.Parent.Parent.MainFrame.Inventory

script.Parent.MouseButton1Click:Connect(function()
	local Result, Message = RS.RemoteEvents.PetActionRequest:InvokeServer("Craft", {PetID = script.Parent.Parent.Parent.PetID.Value})
	if Result ~= nil and Result ~= "Error" then
		
		for _, sus in pairs(Inventory:GetChildren()) do
			if sus.Name ~= "UIGridLayout" and sus.Background.BackgroundColor3 == Inventory.Parent.SelectButtonColor.Value then
				sus:Destroy()
			end
		end
		
		local Disabled = false
		script.Parent.Parent.Parent.PetID.Value = 0
		for i,v in pairs(Inventory:GetChildren()) do
			if v:IsA("Frame") then
				v.Background.BackgroundColor3 = Inventory.Parent.DefaultButtonColor.Value
			end
		end
	elseif Result == "Error" then
		print(Message)
	end
end)

Снимок экрана 2024-06-15 124051

1 Like

Is this script an “InventoryHandler”?

what’s the value set as?
fhhgjhgkhfkdj

The problem is obviously in your for loop to destroy. Maybe use a better method to detect which pets to destroy. For example If sus:ISA(“Frame”) and sus.Name == PetName. Youre detecting for a color3 to be equal to the value in your SelectButtonColor which is a string. There’s definitely a Workaround.