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!

2 Likes

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.

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

Hey this looks cool, I was wondering how I could implement it with the system I have in mind.

So in my game every player has 4 tools. A sword, Axe, Spear, and Hammer. These are in starterpack or whatever so everyone spawns with them.

Anyways there’s a shop for tool skins. Tool skins are actual models. I have a module script containing all the info for a weapon skin and a rep storage folder of all the models. Here’s an example of the module script structure-

[“Fire Sword”] = { Name = “Fire Sword”;
Type = “Sword”;
ImageId = 12345678;
PurchasePrice = 1234;
WeaponModel = Game.ReplicatedStorage.WeaponModels.FireSword; };

[“Energy Axe”] = {
Name = “Energy Axe”;
Type = “Axe”;
ImageId = 382829374839;
PurchasePrice = 69420;
WeaponModel = Game.ReplicatedStorage.WeaponModels.EnergyAxe;
};
the armory module script would continue from here

So I have a tab for each tool. So a sword tab, axe tab, spear tab, and hammer tab. For this example let’s use the sword tab. So I’ll put a script into the sword tab for loading all the weapon skins.

So we have our script in the sword tab, here’s what it would do-
First it will run a pair loop to loop through the armory module script for any table that’s type = Sword (aka v for this example). Then for every Sword type table it finds it’ll clone a template slot for the inventory and fill it with the info-

ShopTemplate.Name.Text = v.Name
ShopTemplate.Image = v.ImageId
ShopTemplate.PurchaseButton.PurchasePrice = v.PurchasePrice

ShopTemplate.Parent = ArmoryShop.SwordTab

So that handles that, so when a player clicks purchase on a template it’ll check if a player’s currency value is >= ShopTemplate.PurchaseButton.PurchasePrice. If it is then it’ll remove that amount from their currency amount. After that it will insert the template’s table into an InventoryTable. However I need to figure out two things. 1, how to check if a player already owns a skin. 2, how to figure out what table is for that template since rn the script has no way of knowing, maybe something like searching the modulescript for a table with the same name as the template then inserting it into the InventoryTable?

So that concludes the shop system, let’s get down to the inventory system.

So for the inventory system we have a similar system. 4 tabs, a script in each tab to loop through the InventoryTable for the weapon skin tables. Then it creates an inventory slot template for each and all that-

InventoryTemplate.Name.Text = v.Text
InventoryTemplate.Image = v.ImageId
InventoryTemplate.Skin = v.WeaponModel

So when a player clicks equip it should clone the InventoryTemplate’s skin variable which is = to the table’s WeaponModel variable which is = to the actual model’s location in a folder in replicated storage.

Now when a skin is cloned it’s parented to the tool. I need to figure out a way to detect what tool to parent it to. Say we wanted to equip a golden sword skin from our inventory? How do we set it in the template’s equip thing to detect which tool to equip it to? Anyways when a weapon skin is finally parented to a tool it’ll destroy the old skin model and weld the new one to the handle.

Now that raises a couple issues. 1, will the tool model reset when a player dies, if so how do we add back the skin model the player equipped? 2, how do we save the currently equipped skin model after a player leaves and rejoins? 3, how will we update the inventory ui when someone buys a new skin?

1 Like

Wowzer! That’s a lot of info and questions!

Since you are using your own system, I don’t want to overwhelm this thread so if you make your own thread in #help-and-feedback:scripting-support and tag me @HeyWhatsHisFace , I’ll hopefully be able to give you a helpful response and I’m sure others will be glad to give you feedback on your shop system as well with even more solutions.

1 Like

ALRIGHTY AWESOME TYSM!

I’ve been having a headache trying to figure this stuff out lol