Make inventory system

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

Now what I want to do is have 3 different categories in the player gui like this:
image

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)
1 Like

I also encountered another problem. The frame “Weapons” deletes whenever I run this script.

Maybe your script is working if you check some lines for spelling mistakes like in this line.

buttons.Parent = inv.Items.Weapons

Should be like this because you can not parent tables.

button.Parent = inv.Items.Weapons

That fixes a problem, but another problem is that it won’t destroy all the items in the table.

So does it not destroy all of your clones inside buttons when you call refresh?

No it doesnt even destroy any. And I can’t find out why

Have you tried to put print(v) inside the two loops inside refresh(). Maybe that can help.

When ever I print out “v” it shows all the items inside the the table, but for some reason does not destroy them…

Do you want to replace the buttons or destroy them? You directly clone new buttons after you destroyed them inside the function.

Yes this is so the button refresh and if any new value is changed it refreshes into that.

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

Thats the plan, but I wanna get the script down first.

So from reading the post and all comments, the current issue is that the script is not destroying all values within the table?

Could you tell me which line this is on? (I am assuming the refresh?)

Also could you tell me why you want to do this, and what the tables are being used for exactly?

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

That was the problem, I fixed it right when before you replied.

function backpackRefresh()

items = {}

search(player.ItemsOwned.Swords)

refresh()

end

Ok, I hope that were all problems.