Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I save a player's inventory with DataStore?

Hi, I'm kind of a newbie to the whole 'DataStore' thing, so I have a question.

If I created a game that involves a player inventory (for storing hats, weapons, and other items), how could I save that inventory with DataStore? (for different places in a universe) And if I had character creation, how could I save a player's character(s)?

Thank you, -Nen

4 comments

0

Do you mean you don't how to use datastore or don't know how to save that kind of stuff?

0

I can probably figure out DataStore, so mostly I just don't know how to save that kind of stuff.

0

To save this kind of stuff, just save a string full of the items in the inventory, then load it up by looping through the table and giving the players the items

0

Not sure I totally understand, would you mind giving me an example?

2 answers

✓ Accepted answer

As a rewording to eLunate's answer to help you understand:

Represent every "item" in your inventory with a numerical ID Give each item properties or whatever you want, also represented by values

save these values in a table

when you start the game, you just sort through that table and insert those items into the inventory based on the ID and properties of that item; essentially "interpret" the values that you saved.

3 comments

0

Yes, sure, but how would I save a table?

0

Here's how datastore works: You create a unique "key" for each player (based on their user ID, preferably) and with that "key" you can save a table of values using the :SetAsync() method (i.e. datastore:SetAsync(key,tableofvalues)).

0

I'll try it thanks.

Serialization

Once you know what you want to save, saving your stuff is fairly trivial. Serialization works like this: You describe the item you want to save, rather than saving every single bit of data.


In an inventory example, you have to consider how you want to save the Inventory. Is it a bunch of slots? We can use a table to represent that, and each slot can be a new entry. What's in the slot? If it's an item, we have to consider how we can describe that item:

  • What is the item's unique identifier? What is the item class?
  • Was the item randomly generated? What was the seed to generate it?
  • Does the item have a level? What level is it?

Save all the details you need, but not the actual item. When you load it again, you can use those details to rebuild all of the items in the inventory.

5 comments

0

I'm not intending to make a class or level based MMO type game, nor would there be randomly generated items, so that could be a bit of an issue.

0

Doesn't matter. It's not an issue if you know what you're trying to save.

0

Look. The point is, if your game selected it somehow, then you can save how it selected it.

0

I may understand, but I'm not sure if that would work for me.

0

Serialization is how you will do it. I don't care if you think it will work, because it will work if you do it right. Every single item will have something which can identify it, and you can use that information to find it again in your game.

This discussion is preserved as it was. New answers, comments, and votes are closed.