How to make a "X of these exist" feature

I see some games where it is like “214 of these exist” on their pets, how would i make that feature?

1 Like

Create a data store for each pet then increment it each time a player gets that pet

For situations where you may have multiple of a single item, it would be better to store the items in a dictionary where the key is the name of the item and the value is how many you have.

i.e.

{
    ["Basketball"] = 3; -- You have 3 basketballs
    ["Baseball"] = 5; -- You have 5 baseballs
    ["Football"] = 0; -- You have 0 (Your choice whether to show this in the UI or not
}

If you want to have a value of all-time existing pets, you’d create a store and increase/decrease when a pet is either obtained or lost somehow. This value should be a number (integer).

To achieve this, you could use:

  • Memory Store
    This allows you to save a table of pets across all servers, you could grab this every minute or whatever the request quota is at the time, and update a table on each server. You can easily read from that table and update it when needed.

  • Data Stores
    You could use Ordered Datastores if you want to have a nice ordered list of all the pets there for you. You can do the same exact process as listed above for Memory Store.

I’d call the datastore whenever you’re able (according to current limit for the server) you could make a system that checks that for you, adds requests to a queue if the limit was reached for the server which is usually really rare, but doing this ensures safety of the values as long as the game instance doesn’t shut down.

The value saved to the datastore should be a key based on the pet type, for example, you could save a key that is named ‘Dogs’ to an Ordered Datastore named ‘Existing Pets’ and increment that by +1/-1 whenever you gain/lose a pet.

1 Like

cant i only use setasync a certain amount of times per minute though?

Memory Stores are not used for saving permanent data, and shouldn’t be. Memory Store is for saving temporary data, like if you need to publish an item to a global marketplace, or create a server list. Data in a MemoryStore will be automatically deleted after 30 days.

You only really need to be saving data on an auto saving timer, I use around 200 seconds. You also need to save when the player leaves or when the server shuts down. You do not want to be calling datastore every time data is edited, and you shouldn’t need to make your own queue system if you’re doing datastores correctly.

You really only want to use OrderedDatastore’s when you’re saving something you NEED ordered, like a leaderboard. Also, you should be saving all your data in a dictionary under 1 key, to prevent hitting limits.

For example, in a GlobalDataStore you’d save the data under a constant key, something to do with their UserId, and you’d set it with something like:

{
["Money"] = 50;

["Pets"] = {
    ["Dog"] = 1;
    ["Cat"] = 5;
    }
}

Even if I use a datastore, how would set and get in every server without call limits and having the value be global

Like I said, get when they join only, set every 200ish seconds, when they leave, and when the server shuts down. Doing it like this means you basically never hit limits.

You don’t set the data when you make a change to it, when you want to edit the data you just make an edit to that table, and that’s what you use to save.

Its pretty simple but complicated in the same time, to start off the pet will be owned by 0 people so, local dog = 0 or make a big dictionary… Now when someone gets it, it MUST add a 1 how is that? MessagingService is your best friend! There is alot of tutorials out there so follow them, but all you need to do is dog += 1 when its in a player’s inventory just send it with MessagingService so it adds it on all the servers, last but not least save it with datastore so it doesn’t go back to 0, and that’s everything!