Get children of a frame

Short explanation: Hello, I am currently attempting to get the children of a frame and then checking each child for an attribute.

Long explanation: I have been creating an inventory system and one of my tools is a medkit, when I use the medkit I need to remove the item from the players inventory heres a simplified version of how I tried to achieve it.

What I tried:

local Slot = Inventory.Frame.Inventory.Content.Equipped:GetDescendants() -- Get slots
local Item = Slot:GetAttribute("Item") -- Get the attribute

if Item == Target then -- check if the attribute string is the same as the target item
	Slot:Destroy()	-- destroy the slot
end
  1. I need to get the children of a frame
  2. I need to check that child’s attribute for a value
  3. if that childs value is not correct then I need to try the next child

Could pairs possibly be the solution to cycle through the array?

for _,v in pairs(Inventory.Frame.Inventory.Content.Equipped:GetChildren()) do
	if v:GetAttribute("Item") == Target then
		v:Destroy()
        break
	end
end

Use the :GetChildren method that returns an array of all the object’s children

loop through each and check

for i,v in pairs(frame:GetChildren()) do
  if v:GetAttribute('Item') then
    v:Destroy()
  end
end
1 Like
local Slots = Inventory.Frame.Inventory.Content.Equipped:GetChildren() 

for _, slot in pairs(Slots) do 
	local item = slot:GetAttribute("Item")
	if item == Target then 
		slot:Destroy()
		--add a break here if you know it only exists once
	end
end
1 Like