Deleting the correct item icon when the real item is deleted?

I’m making a mini-map in my game, with icons on it that show up when an item appears. Getting an icon to show up is easy enough, and works properly, but problem arises when I want to delete the icon when the item is gone. I’m honestly not sure how to go about this. Here is the script so far; a local script located inside the icon GUI:

local stage = workspace:WaitForChild("LucidLandscape") --The stage model

stage.ChildAdded:Connect(function(child)
    if child.Name == "Bell" then --"Bell" is the name of the item I want to make an icon for
	    print("new item")
	    local clone1 = script.Parent:Clone() --This clones the item icon template
	    clone1.Position = UDim2.new((child.Position.X / 1050), 0, -0.38, 0)
	    clone1.Parent = script.Parent.Parent
	    clone1.Visible = true
    end
end)

stage.ChildRemoved:Connect(function(child)
    --???
end)

I would do something like deleting the previously set “clone1,” but there’s a chance that another item is added before the first item disappears, so clone1 would be re-set. Because of this, I don’t know how to remove the correct icon once its corresponding item is removed. How might I accomplish this?

theres a way of doing this with getPropertyChangedSignal

local stage = workspace:WaitForChild("LucidLandscape") --The stage model

stage.ChildAdded:Connect(function(child)
    if child.Name == "Bell" then --"Bell" is the name of the item I want to make an icon for
	    print("new item")
	    local clone1 = script.Parent:Clone() --has to be local
	    clone1.Position = UDim2.new((child.Position.X / 1050), 0, -0.38, 0)
	    clone1.Parent = script.Parent.Parent
	    clone1.Visible = true
	    local connection; -- also has to be local
	    connection = clone1:GetPropertyChangedSignal("Parent"):Connect(function()
	    	connection:Disconnect()
	    	connection = nil
	    	clone1:Destroy()
	    	clone1 = nil
	    end)
    end
end)

Hmm, that didn’t seem to work. Is there anything beyond this code snippet I need to do?

hmmm try it now

3000000000000000000000

Weird, nothing seemed to happen still.
I should mention that “clone1” is the clone of the gui element on the minimap, and “child” is the newly added item. I tried changing the “clone1” in the GetPropertyChangedSignal, but that made the entire ChildAdded function stop working, and I’m not sure why! Maybe it has something to do with the items being inside the map model when the map is cloned from ServerStorage into Workspace.

hmm try changing

connection = clone1:GetPropertyChangedSignal("Parent"):Connect(function()

to

connection = child:GetPropertyChangedSignal("Parent"):Connect(function()

A simple way to do this is utilize a efficient system in which you store the items you clone.

When an item is added to your stage, change the name of the clone you create to the name of the child added in the ChildAdded event.

When ChildRemoved is called, you will want to check if the item being removed is found in whatever script.Parent.Parent is utilizing :FindFirstChild()

For example:

stage.ChildAdded:Connect(function(child)
    if child.Name == "Bell" then
	    local clone1 = script.Parent:Clone()
            clone1.Name = child.Name
	    clone1.Position = UDim2.new((child.Position.X / 1050), 0, -0.38, 0)
	    clone1.Parent = script.Parent.Parent
	    clone1.Visible = true
    end
end)

stage.ChildRemoved:Connect(function(item)
   if script.Parent.Parent:FindFirstChild(item.Name) then
     script.Parent.Parent:FindFirstChild(item.Name):Destroy()
   end
end)

Hopes this gives you an idea on how you can start efficiently storing these types of things.