Help with DataStores

Hello DevFourm! I have been working on a game where you can click food to earn money. With this money, you can buy upgrades to get you more money with every click.

Anyways, I want to make a Data Store system to save the player’s data when they leave. I have never really understood how DataStores worked so I need some help.

(I need a datastore to store IntValues)

Thank you :slight_smile:

5 Likes

Just a datastore or with a leaderstat too?

2 Likes

Just a datastore for an intvalue stored in replicatedstorage

1 Like

So DataStores can store either a value or a table to a specific “key”, which we usually use the PlayerID. To make one save an IntValue, it would be “save”(value, key) and to get that value back we can just do “get”(key).

To start with a datastore, you have to create one using :GetDataStore()

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("ThisIsADataStore") --The name does not matter, as long as it is unique. 

-- For our "get" command it is called :GetAsync() and the parameters are the same. 

local data = DataStore:GetAsync(Player.UserId)

-- And finally for our "save" command, it is called "SetAsync" and the parameters are once again the same

DataStore:SetAsync(IntValue.Value, Player.UserId)

Obviously this is not a functioning script as is, but it should hopefully explain the concepts… To put this into practice, you would use GetAsync() as the player joins to see if they have any data saved and SetAsync() as the player leaves to save their data.

1 Like

this realy helped me understand https://www.youtube.com/watch?v=TIZ3qsCX5S0&t=1041s

Thanks, the video helped me learn a lot about datastores! :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.