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