Table not inserting all objects

  1. What do you want to achieve?
    Insert all frame items within “Grid” into the table!

  2. What is the issue?
    It’s not inserting all the objects within “Grid” that are Frames, only 1 object is being inserted which is UIGridLayout.


  1. What solutions have you tried so far?
    Asking for help on discord & creating a bunch of different prints

Script w/in Proximity Prompt:

script.Parent.Triggered:Connect(function(player)
    
    print(player.PlayerGui:WaitForChild("GameInventory").Frame.Inventory.Items.Grid:GetChildren())

    local current_card = player:FindFirstChild("Data_Folder").CurrentCardStorage
    print(current_card)
    local grid = player:FindFirstChild("PlayerGui"):WaitForChild("GameInventory").Frame.Inventory.Items["Grid"]
    print(grid)
    local frames = grid:GetChildren()
    
    local frameNames = {}
    
    while #frames == 0 do
        wait()
        frames = grid:GetChildren()
    end

    for _, v in ipairs(grid:GetChildren()) do
        if v:IsA("Frame") and v.Name ~= "" then
            table.insert(frameNames, v.Name)
        end
    end

    print("Number of frames: " .. #frameNames)

    for _, name in ipairs(frameNames) do
        print(name)
    end
end)

How many items in the table are intended?

1 Like

It depends on how many Frames are child to “Grid” — 10 maximum frames are intended.

The script is checking if the player has at least three items with the same name in their inventory. If they do, the script destroys those items and gives the player a new, upgraded item. If the player does not have at least three items with the same name, the script displays a message saying that the player does not have enough cards to upgrade.

This is the entire script which might explain what I’m trying to do better:

script.Parent.Triggered:Connect(function(player)
	local current_card = player:FindFirstChild("Data_Folder").CurrentCardStorage
	local grid = player.PlayerGui:WaitForChild("GameInventory").Frame.Inventory.Items.Grid-- replace 'Grid' with the actual name of your object

	local frameNames = {}
	for _, frame in ipairs(grid:GetChildren()) do
		if frame:IsA("Frame") then
			table.insert(frameNames, frame.Name)
		end
	end

	-- check if there are at least three frame names in the table
	if #frameNames < 3 then
		player.PlayerGui.GameInventory.DeleteOneMsg.Visible = true
		player.PlayerGui.GameInventory.DeleteOneMsg.Msg.Text = "You don't have enough cards to upgrade."

		wait(2)
		player.PlayerGui.GameInventory.DeleteOneMsg.Visible = false
		else
		if #frameNames == 3 or #frameNames >= 3 then
				print("yes")
		local nameCount = {}
		for _, name in ipairs(frameNames) do
			if nameCount[name] == nil then
				nameCount[name] = 1
			else
				nameCount[name] = nameCount[name] + 1
			end
		end
		-- check if there are three frames with the same name
		local foundMatch = false
		local itemName = nil
		for name, count in pairs(nameCount) do
			if count >= 3 then
				foundMatch = true
				itemName = name
				break
			end
		end

		-- give item to player depending on the name of the frames
				if foundMatch then
					current_card.Value -= 2
			local player = game.Players.LocalPlayer -- replace with actual player object
				if itemName == "Soul Seeker I" then	
					local destroyCount = 0
					for _, frame in ipairs(grid:GetChildren()) do
						if frame:IsA("Frame") and frame.Name == "Soul Seeker I" then
							frame:Destroy()
							destroyCount += 1
							if destroyCount == 3 then
							local event = game.ReplicatedStorage.InvEvents.PickUp
							local item = game.ReplicatedStorage.Cards["Soul Seeker II"]
							--local firstChild = itemModel
							local itemdescription = item:FindFirstChild("Desc").Value
							event:FireServer(item, itemdescription)
								break
							end
						end
					end
			--player.Backpack:FindFirstChild("Item1"):Clone().Parent = player.Character
					elseif itemName == "Soul Seeker II" then
		local destroyCount = 0
					for _, frame in ipairs(grid:GetChildren()) do
						if frame:IsA("Frame") and frame.Name == "Soul Seeker II" then
							frame:Destroy()
							destroyCount += 1
							if destroyCount == 3 then
							local event = game.ReplicatedStorage.InvEvents.PickUp
							local item = game.ReplicatedStorage.Cards["Soul Seeker III"]
							--local firstChild = itemModel
							local itemdescription = item:FindFirstChild("Desc").Value
							event:FireServer(item, itemdescription)
								break
							end
						end
					end
					elseif itemName == "Stealth I" then
					local destroyCount = 0
					for _, frame in ipairs(grid:GetChildren()) do
						if frame:IsA("Frame") and frame.Name == "Stealth I" then
							frame:Destroy()
							destroyCount += 1
							if destroyCount == 3 then
								local event = game.ReplicatedStorage.InvEvents.PickUp
								local item = game.ReplicatedStorage.Cards["Stealth II"]
								--local firstChild = itemModel
								local itemdescription = item:FindFirstChild("Desc").Value
								event:FireServer(item, itemdescription)
								break
							end
						end
					end
					--player.Backpack:FindFirstChild("Item3"):Clone().Parent = player.Character
					elseif itemName == "Stealth II" then
					local destroyCount = 0
					for _, frame in ipairs(grid:GetChildren()) do
						if frame:IsA("Frame") and frame.Name == "Stealth II" then
							frame:Destroy()
							destroyCount += 1
							if destroyCount == 3 then
								local event = game.ReplicatedStorage.InvEvents.PickUp
								local item = game.ReplicatedStorage.Cards["Stealth III"]
								--local firstChild = itemModel
								local itemdescription = item:FindFirstChild("Desc").Value
								event:FireServer(item, itemdescription)
								break
							end
						end
					end
					elseif itemName == "Beast Sense I" then
					local destroyCount = 0
					for _, frame in ipairs(grid:GetChildren()) do
						if frame:IsA("Frame") and frame.Name == "Beast Sense I" then
							frame:Destroy()
							destroyCount += 1
							if destroyCount == 3 then
								local event = game.ReplicatedStorage.InvEvents.PickUp
								local item = game.ReplicatedStorage.Cards["Beast Sense II"]
								--local firstChild = itemModel
								local itemdescription = item:FindFirstChild("Desc").Value
								event:FireServer(item, itemdescription)
								break
							end
						end
					end
					elseif itemName == "Beast Sense II" then
					local destroyCount = 0
					for _, frame in ipairs(grid:GetChildren()) do
						if frame:IsA("Frame") and frame.Name == "Beast Sense II" then
							frame:Destroy()
							destroyCount += 1
							if destroyCount == 3 then
								local event = game.ReplicatedStorage.InvEvents.PickUp
								local item = game.ReplicatedStorage.Cards["Beast Sense III"]
								--local firstChild = itemModel
								local itemdescription = item:FindFirstChild("Desc").Value
								event:FireServer(item, itemdescription)
								break
							end
						end
					end
					elseif itemName == "Loot Finder I" then
					local destroyCount = 0
					for _, frame in ipairs(grid:GetChildren()) do
						if frame:IsA("Frame") and frame.Name == "Loot Finder I" then
							frame:Destroy()
							destroyCount += 1
							if destroyCount == 3 then
								local event = game.ReplicatedStorage.InvEvents.PickUp
								local item = game.ReplicatedStorage.Cards["Loot Finder I"]
								--local firstChild = itemModel
								local itemdescription = item:FindFirstChild("Desc").Value
								event:FireServer(item, itemdescription)
								break
							end
						end
					end
					elseif itemName == "Loot Finder II" then
					local destroyCount = 0
					for _, frame in ipairs(grid:GetChildren()) do
						if frame:IsA("Frame") and frame.Name == "Loot Finder II" then
							frame:Destroy()
							destroyCount += 1
							if destroyCount == 3 then
								local event = game.ReplicatedStorage.InvEvents.PickUp
								local item = game.ReplicatedStorage.Cards["Loot Finder III"]
								--local firstChild = itemModel
								local itemdescription = item:FindFirstChild("Desc").Value
								event:FireServer(item, itemdescription)
								break
							end
						end
					end
					elseif itemName == "Resistance I" then
					local destroyCount = 0
					for _, frame in ipairs(grid:GetChildren()) do
						if frame:IsA("Frame") and frame.Name == "Resistance I" then
							frame:Destroy()
							destroyCount += 1
							if destroyCount == 3 then
								local event = game.ReplicatedStorage.InvEvents.PickUp
								local item = game.ReplicatedStorage.Cards["Resistance II"]
								--local firstChild = itemModel
								local itemdescription = item:FindFirstChild("Desc").Value
								event:FireServer(item, itemdescription)
								break
							end
						end
					end
					elseif itemName == "Resistance II" then
					local destroyCount = 0
					for _, frame in ipairs(grid:GetChildren()) do
						if frame:IsA("Frame") and frame.Name == "Resistance II" then
							frame:Destroy()
							destroyCount += 1
							if destroyCount == 3 then
								local event = game.ReplicatedStorage.InvEvents.PickUp
								local item = game.ReplicatedStorage.Cards["Resistance II"]
								--local firstChild = itemModel
								local itemdescription = item:FindFirstChild("Desc").Value
								event:FireServer(item, itemdescription)
								break
							end
						end
					--end
			end
		end
			end
	end
end
end)

basically, 10 items though, in the table! :slight_smile: