I am trying to make an inventory system, but I’m having some trouble. Right now I have a script that when you open a case it will insert a string value into the player items owned folder.
Now what I want to do is have 3 different categories in the player gui like this:
and for each type of item that the player owns it inserts it into the correct frame, but I am having some trouble.
Heres my script:
local mainButton = script.Parent.Button.TextButton
local items = {}
local buttons = {}
local inv = script.Parent.Inventory
local weaponButton = inv.WeaponButton
local TitleButton = inv.TitlesButton
local trailButton = inv.TrailsButton
local player = game.Players.LocalPlayer
function search(location)
for i, v in pairs(location:GetChildren()) do
if v:isA("StringValue") then
table.insert(items, v)
end
end
end
function refresh()
for i, v in pairs(buttons) do
v:Destroy()
end
for i, v in pairs(items) do
local button = script.Sample:Clone()
button.Name = v.Name
button.Text = v.Name
button.LayoutOrder = i
buttons.Parent = inv.Items.Weapons
table.insert(buttons, button)
end
end
mainButton.Activated:Connect(function()
if inv.Visible == false then
inv.Visible = true
else
inv.Visible = false
end
search(player.ItemsOwned.Swords)
refresh()
end)
weaponButton.Activated:Connect(function()
script.Parent.Inventory.Items.Weapons.Visible = true
script.Parent.Inventory.Items.Trails.Visible = false
script.Parent.Inventory.Items.Titles.Visible = false
search(player.ItemsOwned.Swords)
refresh()
end)
trailButton.Activated:Connect(function()
script.Parent.Inventory.Items.Weapons.Visible = false
script.Parent.Inventory.Items.Trails.Visible = true
script.Parent.Inventory.Items.Titles.Visible = false
search(player.ItemsOwned.Trails)
refresh()
end)
TitleButton.Activated:Connect(function()
script.Parent.Inventory.Items.Weapons.Visible = false
script.Parent.Inventory.Items.Trails.Visible = false
script.Parent.Inventory.Items.Titles.Visible = true
search(player.ItemsOwned.Titles)
refresh()
end)
Instead of creating the GUI from the Table, you could use ChildAdded and connect a function that so it will create a Frame within the correct GUI. I do this in the game that I am currently making.
Put a UIGridLayout inside of the Frame, and code the function to create a new button, or whatever you trying to make for the item; and the UIGridLayout will keep it organized as new ones are created
Ok, I think the issue might be the items table everytime you you call search() you add a new value to the table and so you get another duplicated button. So you have to check if the value is on the table and not add that. Like that
function search(location)
for i, v in pairs(location:GetChildren()) do
if v:isA("StringValue") then
if not table.find(items, v) then
table.insert(items, v)
end
end
end
end