Help With Buying Items from Shop to Go In An Inventory/Backpack

Hi! So I know this topic isn’t new and it’s something that has been posted a lot about. I’ve seen a broad variety regarding inventory systems and whatnot but I need a sort of guided direction because I’m not entirely sure where to start. I already scripted my shop and have one gear set up and the purchasing works fine. You can buy something and it goes straight into your inventory. I just had a question regarding inventory and also the shop purchasing feature. I want to make it where, when you buy an item, it goes into an inventory that isn’t the roblox standard one. One where you click on a backpack/inventory button and the item you purchased is in there. And then from there you can unequip and re-equip if needed. I also want to have it where you can’t buy the same item from the shop again. Like maybe in replace of the “buy” its just “owned” instead so you can’t buy again.

I’ll include some scripts I have of my shop so far.

game.ReplicatedStorage.ToolEvent.BearEvent.OnServerEvent:Connect(function(player)
	if player.leaderstats.Points.Value>10 then
		player.leaderstats.Points.Value = player.leaderstats.Points.Value - 10
		game.ServerStorage.Tools["Teddy Bloxpin"]:Clone().Parent = player.Backpack
	end
end)

This is located in the ServerScriptService.

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.ToolEvent.BearEvent:FireServer()
	
end)
script.Parent.MouseButton1Click:Connect(function()
	game.Workspace.ching:Play()
end)

This is the script in my Shop Gui to fire the event to purchase.
I also have my tools stored in ServerStorage.

Any help is appreciated! Whether that is by links to posts on the devforum that would help guide me or tutorials or even scripting help. Like I said, I have searched on here and found some tutorials for an inventory system but they seemed more geared toward picking up items and storing and not just shop purchasing. Thanks in advance!

1 Like

Sounds like you already have your shop done but want a custom UI to go along with it. Depending on your scope/use-case, there’s a lot that goes into designing and programming a custom UI. I’ll give you some starting points but since this place isn’t meant for users to write your code, you’ll have to adapt these starting points or find better ones on your own. There are many resources and free models out there that you can look at that will help you form a good approach. Here’s an easy-to-follow resource that only took a quick Google search.

Duplicate Buying Restrictions

You can make conditionals in your shop to check to see if they have it in their inventory before allowing the player to make a purchase.

Example:

if PurchasingPlayer.Backpack:FindFirstChild(ItemName) == nil then --Conditional to make sure the player doesn't have the item before proceeding with purchase.

Disabling the Standard Backpack

To disable the default Backpack UI, you need to use this line from the client/LocalScript:

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

Custom Backpack/Inventory

You can still use the same location (Player.Backpack) or make your own custom location for items. I’d suggest doing a custom location because the default Backpack has the tool removed/relocated every time it’s equipped. By making your own equip function, you can equip a clone, rather than relocating it.

  • Design your UI however which way you want and you can bind it to a button that opens and closes the inventory/backpack using the MouseButton1Clicked event. It’s as simple as changing the Visible property of the Frame. You can even make it popup fancy by using Tweens. Here’s a good intoduction.

  • Once you’ve created your UI design, you have to populate it with frames for every tool. You can parse the inventory location using loops.

local ToolList = {} --Defines a table for the list of tools
function UpdateToolList()
	ToolList = {} --Clears toollist
	for i,v in ipairs(InventoryLocation:GetChildren()) do
		if v:IsA("Tool") then
			table.insert(ToolList,v)
		end
	end
end
  • With every tool you find from the loop, create a button representing each tool. Then you can bind equip/unequip functions to each button’s click event.
function CreateButtons()
	for i,v in pairs(ToolList) do
		if v then --Depending on when CreateButtons() is called, the ToolList may contain nil values if a tool is somehow deleted/destroyed.
			local Button = ButtonDesign:clone()
			Button.Name = v.Name
			Button.ButtonImage.Image = v.TextureId
			Button.Parent = CustomToolboxUILocation
			Button.MouseButton1Click:connect(function()
				local Equipped
				local Character = LocalPlayer.Character
				if not Character then return end --Doesnt proceed if it can't find the character. 
				if Character:FindFirstChild(v.Name) then --Searches the character for the tool.
					Equipped = true
				else
					Equipped = false
				end
				if Equipped == true then
					UnequipTool() --link to unequip function
					UnhighlightButton(Button) --link to unhighlight function
				else --If its not equipped
					EquipTool() --link to equip function
					HighlightButton(Button) --link to highlight function
				end
			end)
		end
	end
end
  • Then when the players backpack has an item removed/added, remove the current buttons, update the ToolList, then make new buttons.
function UpdateUI()
	CustomToolboxUILocation:ClearAllChildren()
	local NewUILayout = Instance.new("UILayout",CustomToolboxUILocation) --Assuming you are using a UILayout, replace UILayout with UIGridLayout or UIListLayout depending on your UI design. This replaces the old one that got destroyed with :ClearAllChildren()
	--set the UILayout back to the original settings. Example NewUILayout.Padding = Vector2.new(0,5), etc., etc.
	UpdateToolList()
	CreateButtons()
end

InventoryLocation.ChildAdded:connect(UpdateUI)
InventoryLocation.ChildRemoving:connect(UpdateUI)

This method is just one approach to how it could work. There are many other ways of creating custom Toolbars/Backpacks/UI. You’ll have to come up with your own solution tailored to what you want. With this example, it refreshes the UI every time the players backpack gets changed. The UI positioning of the tools may get rearranged during every inventory change because of that. A workaround is to save the locations in the UI by putting their location/order into a table, then referencing the table when creating buttons so you can assign their Position/LayoutOrder in the CreateButtons() function. You can even create empty buttons for unassigned/empty slots if you wanted to. Just needs a bit of creativity and forethought.

2 Likes

Also you could Save the inventory by saving the names of the items in the Inventory, then when loading there data just have a separate folder in server storage with all the items and then clone them in the inventory if they have it as your saving a table as the data

1 Like

Yeah I already have my shop set up and as for the resource you linked, I did actually watch that one but it wasn’t exactly what I was looking for. I am a beginner scripter so I am trying new things out as I go, learning new things, and everyone on here has been immensely helpful with providing direction and help when needed to be quite honest. That being said, thank you for your in depth explanation. It really puts in perspective certain information that I was searching for and it made me understand it a lot more as well. This will definitely help me. Thanks again!

Alright thank you! I will try that out.