Need help with inventory and equipping system

@HeyWhatsHisFace, Help With Buying Items from Shop to Go In An Inventory/Backpack - #2 by HeyWhatsHisFace

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?

5 Likes

@HeyWhatsHisFace , here ya go, I posted it!

Use Datastores. And when player buys a skin or anything, store that data into a table inside the Script or a Module. Like

local PlayersData = {}
-- player id key
PlayersData[1564894665] = {
	Tools = {
		Sword = {"Fire", "Water"}
	}
}

Its just a basic example, you should build your tables according to your needs
Or store values in Player instance referencing its tools and skins.

So when player leaves you grab the information in table or the info from values in player, and save it into player’s Datastore

When your script creates the buttons per item/skin, you should connect an .Activated event to the button, the function will use the Information gotten from the loop from each iteration, so each button points out/reference each Sword table, so each button knows the source table where its data came from.

Put the new model into PlayerGear so everytime player respawn, it comes with the previous tool and skin that chosed before.

Datastores

I dont understand this one, what you mean? With whatever you like, idk, turn the color of the button of that item into a red/orange

1 Like

Like @Dev_Peashie said above, you’ll want to store the purchase information inside of a table, which you’ll save to DataStores.


You’ll have to give it a way for the script to know. You can do this by checking for a tag or property within the table. For example you have a Type key in your table that you can check to tell the script what each table is for.


I don’t recommend this. You should equip the skin based on player choice instead. So for example the Sword Skins might be
Normal (Equipped)
Fire
Ice
Magma
Then when the player chooses to equip a different skin, if they don’t own it, they can be prompted to purchase it, which will get saved to that DataStore we mentioned earlier. Then once they own it, it automatically replaces the weapon skin as well as every time they spawn.

You can do this many ways, @Dev_Peashie suggests using PlayerGear, which is a straight forward approach. You’ll just need to remove the old version of the tool.


When someone successfully purchases an item, it’ll be up to your script to tell the rest of your functions a successful purchase was made. An easy way to do this is with BindableEvents or RemoteEvents. It all depends on your setup and how you are designing your functions. A more integrated solution would be yielding your function for a purchase result then changing the UI based on the result.

I hope I understood what you were getting at, there were a whole bunch of questions there that I tried my best to wrap my mind around. :sweat_smile:

So basically all the tools are in starter gear or whatever it’s called so everyone gets them, right. So what you’re saying is all I’ve gotta do is have the new model skin parented to the tool and everything and they’ll still respawn with that as their model skin right?

1 Like

Yeah I think I just did a horrible job at explaining everything since there’s so much going on, most of these are helpful but aren’t what I’m trying to achieve. Do you have disco so I can show you it more visually and explain it better in its entirety?

1 Like

Well, StarterPack the folder in your game, when a player joins, those tools will be cloned and placed in the player’s Backpack.

Player Instance has a Backpack “folder”. And Player has a folder called StarterGear too, which is like a persistant Tool for only that Player that when dies it will be given again to that Player and only that Player, the other players that join game will only receive the Tools in StarterPack, not the Tools that are inside StarterGear of other player, its an “individual” folder.

But, honestly I never tried that approach. I guess that when player reSpawn will receive the Tool in the StarterPack and the Tool in StarterGear too, so… 2 Tools, one with the custom Skin and one being the original that everyone receives on join. So you would need to destroy the one you dont want the player to have… not efficient at all

I prefer handling everything with tables and values. Its more flexible. If you have a PlayersData table, you can store there what tools the player is using, skins etc. Same if you place values in players to keep track of what player is using, so upon respawn you can give the proper tool to player

1 Like

Okay @Dev_Peashie @HeyWhatsHisFace I just did a bad job at explaining everything and what I’m trying to achieve. Give me a minute all I’ll do my best to explain what I’m trying to do

1 Like

@Dev_Peashie @HeyWhatsHisFace Sorry for the wait, im trying to go in depth as possible and I’m also waiting on my ui artist for some assets so I can also have visual aids to show you guys

1 Like

So im making a combat game similar to Zo but with a few differences. First off players have all 4 weapons available to them as tools at all times. Players get “Credits” from kills and can spend them on weapon skins in a shop.

INTRODUCTION TO THE SHOP LAYOUT
Here is the shop layout with empty templates-

As you can see there’s a tab for each weapon type (ignore characters). So let me explain the layout of a shop template-


There’s 4 major parts to a shop template:
⁃ The skin price (ShopTemplate.SkinPrice)
⁃ The weapon type (ShopTemplate.WeaponType)
⁃ The skin name (ShopTemplate.Name)
⁃ The skin image (ShopTemplate.Image)

BASIS OF HOW THIS MIGHT WORK PT. 1
So all the weapon skins that will appear in this shop are in a modulescript called ArmoryModule

local module ArmoryModule = { 

[“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 module would continue on past here, this is just a few examples of skin tables*
}

Outside of this module the physical weapon skin models are in a folder in replicated storage called WeaponModels, which is referenced in each table’s WeaponModel variable.

BASIS OF HOW THIS MIGHT WORK PT 2
So how do we get these skins to show up in the shop you may ask? Well here’s the approach I was thinking. For this example let’s use the Sword skin tab of the armory shop.

First, we’d insert a script into the sword shop tab

Second, we could run an in pairs loop to cycle through the ArmoryModule for any tables with their Type equaling sword (Type = Sword). When it finds these tables (let’s use v as a placeholder for these tables) it’ll clone a template shop slot for the item and fill out it’s info using the table and parent it to the shop tab-
(image of a template)
image

Third, it will set the cloned template’s info to the table’s info like this-
ShopTemplate.SkinPrice = v.PurchasePrice
ShopTemplate.WeaponType = v.Type
ShopTemplate.Name.Text = v.Name
ShopTemplate.Image = v.ImageId

Fourth, RESULT, your templates should now look like this (example)

PURCHASING PT.1
So I’m going to give a brief rundown how the basis of how the purchase system will work since I still need some help on making the system code itself.

So when a player wants to purchase a skin they’ll hold it down and this grey bar will slowly fill over the course of 5 seconds-

So when that bar gets full the system will attempt a purchase. It’ll check if a player’s Credits (our currency) value is greater than or equal to the Template.SkinPrice and if it’s not already in their inventory. If it is then it’ll subtract the Template.SkinPrice value from their currency value. After that it somehow needs to take the table it was created from and insert that table into an InventoryTable (ONE OF THE THINGS I NEED HELP FIGURING OUT), which is our next topic.

PURCHASING PT.2
Now, we know how we want it to work, but we have a few problems to solve-

Problem 1:
So we addressed earlier how the shop system works. It loops through the modulescripts, clones a template, and changes the template info based off the tables. So when a player buys a skin it needs to somehow figure out what table was used to create it and insert it into an inventory table. So let’s say we had a Fire Sword skin in the shop. When a player buys this we want it to find the table used to create that template, then insert that table into our InventoryTable. So when a player buys the Fire Sword skin it needs to somehow go into the modulescript and find the table called Fire Sword, then insert that table into our inventory table.

Problem 2:
We don’t want players buying duplicates of skins, so what’s the best way to check if a player already has a skin in their inventory so they can’t purchase it again?

INVENTORY SYSTEM PT.1
Here’s the inventory layout with some blank templates-

Much like our armory shop there’s tabs for every tool. Ignore the big side bar to the right and the search bar for now.
So let me explain the layout of an inventory template-

There’s 3 major parts to an Inventory template:
⁃ The weapon type (InventoryTemplate.WeaponType)
⁃ The skin name (InventoryTemplate.Name)
⁃ The skin image (InventoryTemplate.Image)

BASIS OF HOW THIS MIGHT WORK PT. 1b
So earlier with our shop we established that when a player buys a skin it’ll insert its table into a big InventoryTable, so it would look something like this-

InventoryTable = {the tables of skins we’ve purchased would be here}

BASIS OF HOW THIS MIGHT WORK PT. 2b
So, the system for the inventory is similar to the shop. For this example let’s use the Sword tab of the inventory.

First, we’d insert a script into the sword tab of the inventory.

Second, we could run an in pairs loop to cycle through the InventoryTable for any tables with their Type equaling sword (Type = Sword). When it finds these tables (let’s use v as a placeholder for these tables) it’ll clone a template inventory slot for the item and fill out it’s info using the table and parent it to the sword inventory tab-
(Image of an inventory template)
image

Third, it will set the cloned template’s info to the table’s info like this-
InventoryTemplate.WeaponType = v.Type
InventoryTemplate.Name.Text = v.Name
InventoryTemplate.Image = v.ImageId

Fourth, RESULT, your templates should now look like this (example)
image

EQUIPPING SKINS PT.1
So we’ve established that everyone all has the same 4 weapons and that they’re tools. So let’s go over what we want to ultimately accomplish-

What we want to accomplish:
When a player presses on a slot in the inventory it’ll take that skin, clone it, parent it to the player’s tool, destroy the old tool model, and weld the new one. We also want the equipped skin to be saved when a player leaves & rejoins, or when a player respawns.

Now that we’ve established what we want to achieve let’s address a few problems-

PROBLEM 1:
How will a player actually equip a skin? Our script doesn’t know what model to clone. We need to somehow reference the table used to make that inventory template and its WeaponModel variable. One way I was thinking is when we’re cloning the inventory templates and giving it our table info we somehow give it our WeaponModel Variable info. Here’s an example

InventoryTemplate.WeaponModel = v.WeaponModel

Our tables have a WeaponModel variable which I mentioned earlier, this variable directly gives the location of the model, for example WeaponModel = game.Workspace.ReplicatedStorage.WeaponsFolder..

So basically what we’re doing is taking the table’s weapon model variable and setting a template’s variable to that. So when a player clicks equip it’ll clone the InventoryTemplate’s WeaponModel variable, which is the model we want.

PROBLEM 2:
How will our script know what tool to parent it to? I have an idea for a solution, an if statement. So when a player clicks equip it’ll run an if statement checking the weapon type of a skin, here’s the example-

if InventoryTemplate.WeaponType = Sword then
WeaponModel:Clone()
Clone.Parent = (Our Sword Tool)
(All the code here for the welding, destroying, etc for the skin model)
elseif InventoryTemplate.WeaponType = Axe then
WeaponModel:Clone()
Clone.Parent = (Our Axe Tool)
(All the code here for the welding, destroying, etc for the skin model)
elseif InventoryTemplate.WeaponType = Spear then
WeaponModel:Clone()
Clone.Parent = (Our Spear Tool)
(All the code here for the welding, destroying, etc for the skin model)
elseif InventoryTemplate.WeaponType = Hammer then
WeaponModel:Clone()
Clone.Parent = (Our Hammer Tool)
(All the code here for the welding, destroying, etc for the skin model)
end

Also when it comes to equipping we need it to destroy the old model for the tool/weapon, how would we go about this?

PROBLEM 3:
So we established how the equipping system should work, but how do we only have it change the model for just that player? The weapons are in StarterPack so everyone will receive them on join/respawn. But since everyone gets them, that would mean if we changed the weapon’s model then it would do the same for everyone else right? What’s a good solution to only changing the model for the player and not everyone else?

PROBLEM 4:
Now I don’t know if this will be an issue but I’m making a prediction. If a player dies will the tool’s models reset to the default model? If so, what’s a solution?

PROBLEM 5:
So in our inventory ui when a player equips a skin it’ll change the border color and put a checkmark (checkmark asset isn’t available rn) over it-
image


How would we do this in particular? I have no idea on how to approach this. We also want this to show up as equipped (border color change and checkmark) when a player rejoins, so how do we save that?

PROBLEM 6:
We want player’s equipped skins to save and load when they leave/rejoin a game right? What’s a good way to have their equipped skins save and be applied when they join a game?

PROBLEM 7:
How do we load newly purchased skins in the inventory? Like say someone has purchased a skin while ingame, how will we update the inventory gui to show it?

Hopefully this helps explain everything in full detail, before we go over the problems feel free to ask any questions or concerns you may have!

1 Like

@HeyWhatsHisFace @Dev_Peashie I hope this helps explain things! Let me know if you have any questions then we can address the problems one by one.

@HeyWhatsHisFace @Dev_Peashie, hey just making sure you saw this just in case you didn’t get the ping notification the first time

Add me on discord, this will make it easier for me to help you.

Country Coder#2633

I have to go into the office in the morning but I can respond when I have time. I am heading to sleep, gotta be up at 6, its 12 right now.

Oh thanks! I’ve been having a hard time trying to figure out this system, so any help is appreciated!