Script not working

Hello, I made a script and two things are not working: The Clone() function is not cloning the GUI, and this function returns nil

	for _, i in pairs(SlotsUsed) do
		if i == false then
			i = true
			InventorySlot.Number.Text = #SlotsUsed[i]
		end
	end
game.ReplicatedStorage.Events.SearchTrashEvent.OnClientEvent:Connect(function()
	local Player = game.Players.LocalPlayer
	local RandomItem = math.random(1,10)
	local InventorySlot = Player.PlayerGui.MainGUI.HotbarDisplay.Template
	local Inventory= InventorySlot:Clone()
	Inventory.Name = ItemDatabase[RandomItem]
	for _, i in pairs(SlotsUsed) do
		if i == false then
			i = true
			InventorySlot.Number.Text = #SlotsUsed[i]
		end
	end
	Inventory.Icon.Image = ""
	Inventory.Visible = true
end)
for _, i in pairs(SlotsUsed) do
    if i == false then
		i = true
		InventorySlot.Number.Text = #SlotsUsed[i]
	end
end

A for loop is used to loop through a table. A for loop returns two values, _ is the index and i is the value.

So first of all, yes, you are looping through a table, but I don’t think SlotsUsed is even defined as I don’t see it’s reference anywhere in the script.
Second of all, you are trying to check if the index is false and set it to true if it is, which isn’t possible.

Here’s a fixed for loop:

for index, value in pairs(SlotsUsed) do --ipairs is faster but less compatible with dictionaries
    if value == false then
		value = true
		InventorySlot.Number.Text = #SlotsUsed[index]
	end
end

You probably should have also provided more information, where the script is located and what type of script it is, a local script or a server script.

In addition, you had problem with cloning the GUI. It definitely is. When you clone an instance, it’s parent is set to nil so you have to parent it to an instance and it’ll be visible in the hierarchy.

Hope this helped, if something’s up, reply to me.