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)
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.