How do I make a tool shop system?

What I am trying to achieve

Hello, I am making a game but I require a shop system where the player can buy the tool and get it in their inventory where they can equip it and use it as their current weapon for a next round. However, I ran into problems while doing that.

What I can do

I am very confident that I can make the shop itself with the buying system and storing it in the player’s inventory, but I don’t know how to make equipping the tool possible and also how to save the player’s inventory with DataStore2.

What I tried doing

To try and store the player’s tools with DataStore2, I tried to directly store the instances, and realized in a few moments after trying it that it wouldn’t work. I also tried to make sure the player gets the “Default” tool when they join the game if they didn’t have tools and this is what I did;

if dataWeaponsOwned:Get()==nil or dataEquippedWeapons:Get()==nil then
  local default_weapon=game.ServerStorage.Default:Clone()
  local default_weapon2=game.ServerStorage.Default:Clone()
  default_weapon.Parent=OwnedWeapons
  default_weapon2.Parent=EquippedWeapons
else
  dataWeaponsOwned:Get().Parent = OwnedWeapons
  dataEquippedWeapons:Get().Parent = EquippedWeapons
end

Assuming OwnedWeapons is the folder inside the player that contains the weapons the player owns and EquippedWeapons is the folder where the equipped weapon is stored.

I tried this and it didn’t really work. There was no error but it hit me that DataStore2 can’t store instances, at least that is what I think.

Are there any other ways to do this? Remember that I want to use DataStore2 to store the owned weapons AND the equipped ones. How would you do this?

2 Likes

Not sure what you mean by this, could you elaborate?

As for the data saving, I would suggest one of the following things:

  1. You could save a table of strings for the weapons the player owns:
    {"Sword", "Spear", "etc"}

  2. You could save a dictionary of each weapon, and then set whether they own it to true or false:
    {Sword = true, Spear = false, etc = true}

Of course, you would have to design a system to load which weapons the player owns, but that shouldn’t be too bad.

For saving the weapons the player has equipped, just use the first option I showed you, but instead of saving what weapons the player owns, just save the weapons they have equipped.

I’m gonna have to design the exact same system for my game :sweat_smile:

1 Like

You cant store instances (at least I don’t think so) rather you should store whatever item the player has equipped as a string in a table if necessary (like tool.Name), then whenever a match starts or ==, fetch the equipped data, and clone().parent the item to the player / backpack that is = to the equipped string.

Thanks for the explanation! This will help out a lot.

By equipping the tool I mean putting the tool in a folder so the game knows that it is the tool the player wants to use every round. The player should be able to change what tool they want to use in a round.

1 Like

You can take a look to my open sourced tool shop.

I have done that kind of stuff before where I look at other people’s things but I end up copying the scripts directly which is not what I want. This game I’m making could be successful and I want this to be a learning process for me. Thanks for telling me though, I’m sure others that come to this post may see this would get help for it.

I fully understand you, but my main purpose was to you to look on the code and take inspiration on how I done it and how you can approach something similiar too!

(There’s nothing bad about it)

I am really tempted to look inside it but I will end up copying and later claiming it as my own code which has put me in a lot of situations before my rebranding. I appreciate it though, I am sure to share that module with my friends that are eager to script.

2 Likes

Oh, that’s actually pretty easy. You just parent a clone of the tool to the player’s backpack. (Tool.Parent = Player.Backpack). If you want to change it, just destroy the current clone and parent a clone of the new tool to the player’s backpack. (You have to clone it because multiple players might want the same tool.)

1 Like