Loops Not Working

Greetings! I have been working on a trash can system, which is decently advanced, however, it doesn’t work without any errors. This is the script which isn’t working:

local Player = game.Players.LocalPlayer
local SampleItem = script.Parent.SampleButton
local Backpack = Player.Backpack
local Items = Backpack:GetChildren()
local ItemsInInventory = {}

for i = 1, #Items do
	local CurrentItem = Items[i]
	
	table.insert(ItemsInInventory, CurrentItem.Name)
	return ItemsInInventory
end

for i = 1, #ItemsInInventory do
	local CurrentIteration = ItemsInInventory[i]
	local ItemName = table.find(ItemsInInventory, CurrentIteration)
	local Clone = SampleItem:Clone()
	
	Clone.Text = ItemName
	Clone.Name = ItemName
	Clone.Parent = script.Parent
	Clone.BackgroundTransparency = 0
end

Please do help!

Hello? Does anyone know how to fix it?

table.find returns the index in which the value was found, not the value itself. Instead it should be replaced with local ItemName = ItemsInInventory[i].

Also to clean the script, you can make the last part a for i, v loop:

for _, item in pairs(ItemsInInventory) do
	local Clone = SampleItem:Clone()
	Clone.Text = item
	Clone.Name = item
	Clone.Parent = script.Parent
	Clone.BackgroundTransparency = 0
end

PS: when errors occur, it helps to reference them inside your topic.

It still does nothing with no errors, as stated up in my topic. :thinking:

What are you trying to achieve? And what’s exactly not working? Also, there’s no need to create an array here, you can just use an iterative loop.

1 Like

So basically, it looks through the player’s inventory, adding the name of the tools 1 by 1 into a table. From there, it clones a TextButton, and names it, and changes the text of it to that tool’s name. There is a connected script to this, but it first needs this script.

I assume the script is a LocalScript, where is it parented(which service)? It may be due to the script not listening for inventory changes.

It is a LocalScript, it is parented to a ScrollingFrame which holds the SampleItem. It is also in StarterGui.

First of all as @Sarchyx correctly said, the extra loop isn’t needed:

local Player = game.Players.LocalPlayer
local SampleItem = script.Parent.SampleButton
local Backpack = Player.Backpack

for _, item in pairs(Backpack:GetChildren()) do
	if not item:IsA("Tool") then continue end 
	local Clone = SampleItem:Clone()
	Clone.Text = item.Name
	Clone.Name = item.Name
	Clone.Parent = script.Parent
	Clone.BackgroundTransparency = 0
end

Secondly the script may run before the tools are added and it never updates, instead try listening for tool changes:

local Player = game.Players.LocalPlayer
local SampleItem = script.Parent.SampleButton
local Backpack = Player.Backpack

function Update()
	for _, item in pairs(Backpack:GetChildren()) do
		if not item:IsA("Tool") then continue end
		local Clone = SampleItem:Clone()
		Clone.Text = item.Name
		Clone.Name = item.Name
		Clone.Parent = script.Parent
		Clone.BackgroundTransparency = 0
	end
end

Update()
Backpack.ChildAdded:Connect(Update)
Backpack.ChildRemoved:Connect(Update)
local SampleItem = script.Parent.SampleButton
local Backpack = Player:WaitForChild("Backpack")
local Items = Backpack:GetChildren()


for i = 1, #Items do
	local CurrentIteration = Items[i]
	local Clone = SampleItem:Clone()
	
	Clone.Text = CurrentIteration.Name
	Clone.Name = CurrentIteration.Name
	Clone.Parent = script.Parent
	Clone.BackgroundTransparency = 0
end

Now it says a script is a tool?

Try adding if not item:IsA("Tool") then continue end before the local Clone line, you may have parented a script inside the player backpack.

Yeah, I did, now I’m back at square one; It does nothing.

The WaitForChild may have an Infinite Yield Possible error, hence me not using it.

Take in mind pre-cloned buttons will not get destroyed, before updating make sure to destroy any existing buttons.

local PLS = game:GetService("Players")

local UI = script.Parent
local Sample = UI:WaitForChild("Sample")

local Client = PLS.LocalPlayer
local Backpack = Client.Backpack

local function Update()
	for _, button in ipairs(UI:GetChildren()) do
		if not button:IsA("TextButton") then continue end
		button:Destroy()
	end
	
	for _, tool in ipairs(Backpack:GetChildren()) do
		if not tool:IsA("Tool") then continue end

		local Cloned = Sample:Clone()

		Cloned.Text = tool.Name
		Cloned.Name = tool.Name
		Cloned.BackgroundTransparency = 0
		Cloned.Parent = UI
	end
end

Update()

Backpack.ChildAdded:Connect(Update)
Backpack.ChildRemoved:Connect(Update)

I’ve tested this and works pretty well:

1 Like

I guess I’ll think of a different way of doing this, it doesn’t really work at all. Thanks for the help!