I need help with DataStores

So i ve been trying to learn how to use DataStores to save and load values but I’m not sure how to use DataStores. It would be nice if someone could teach me how to do it properly.

  1. Folder With Values

E
I marked types of values after Green, Red and Yellow

Green marked Values are String Values

Red marked Values are Bool Values

and yellow Values are Number Values

so the point is I’m not sure how to save or load values with a Script

I made a script on player enter that clones and sets the parent of the folder to player.leaderstats

I just need a template of a script that can load or save values like these

I can’t give you the code to do this, as it won’t be good for you to put direct code into your game without learning it. But I can tell you how you could do it in one way, what you can do is Run a loop through the stuff you want to save, Insert the key & value to a table, and then after the Loop has ended you just need to Set that value to the datastore and its saved.

Then when you need to do load it you can do a loop again, to create new value instances and give the correct value.

Creating, saving and loading DataStores is pretty easy. Let me give you a small example:

local DSS = game:GetService("DataStoreService") -- we get the DSS
local MoneyStore = DSS:GetDataStore("MoneyData") -- here, we get a "Store". You can call it however you prefer, I called it "MoneyStore" so I'll remember it stores the player's money data. In the brackets, you can find the key we gave for that specific data: in this case, it's "MoneyData".

    game:GetService("Players").PlayerAdded:Connect(function(plr) -- this fires when a new player joins the server, which is flagged as "plr"
           local leaderstats = Instance.new("Folder") -- this is the leaderstats folder
           leaderstats.Name = "leaderstats"
           leaderstats.Parent = plr -- we set the leaderstats to the player
           local moneyVal = Instance.new("IntValue") -- we create a new IntValue here
           moneyVal.Name = "Money" -- we set the name of the value we made. You'll need to remember the name and path of this IntValue
           moneyVal.Parent = leaderstats -- we set the parent of the IntValue to the leaderstats folder
           moneyVal.Value = MoneyStore:GetAsync(plr.UserId) or 0 -- this might look confusing, but let me explain: firstly, we GET (GetAsync) the data from the player's UserId (plr.UserId). I don't recommend using plr.Name as if the player changes name, they will lose their data. Then, we are basically telling that if the store doesn't find any data for that player, then the data will be set to 0 (or 0)
    end)

Now that we’ve made the part where the player’s data is loaded or created, then we can make the part where the player’s data is saved. We have two options here:

  • Set the new data everytime the value changes (Not recommended, as if the value is changed consistently, then you’ll get an error). This should go under the PlayerAdded event:
     moneyVal.Changed:Connect(function(newVal)
         MoneyStore:SetAsync(plr.UserId, newVal) -- we set the new data saved on the player's UserId as the key, and the Money as the value. Here we don't need to write down the path of the IntValue as the value is still defined under the PlayerAdded event.
     end)
  • Or (best option) is to save the data when the player leaves:
    game:GetService("Players").PlayerRemoving:Connect(function(plr)
           MoneyStore:SetAsync(plr.UserId, plr.leaderstats.Money.Value) -- same thing as the first method, except this time we need to write down the path of the value, as it's not defined under this PlayerRemoving event
    end)

That’s a pretty basic tutorial, you’ll have to implement with your game tho.
For more informations just visit the DevsHub article (Data Stores | Documentation - Roblox Creator Hub) or look up for a tutorial on YouTube.
Hope this helped :slight_smile:

2 Likes

No, these are the best options:

  • Load data when player joins
  • Save every X minutes
  • Save on PlayerRemoving
  • Save when shutdowns occur (BindToClose)

Never save data when a value is being changed, as this will most likely break your datastore if it’s set too frequently. Read more about the Roblox DataStore limits here

1 Like

I know about the DSS limits, that’s why I did put the reason he shouldn’t use the first method under (). About your methods, yes I should’ve mentioned the BindToClose. Saving every X minute isn’t necessary and it just adds additional code. I didn’t wanna go in depth tho as it looks like this is the first time he uses DataStores. You shouldn’t say “No, this are the best options” as everyone has their ways. I personally rather use DataStore2 than Roblox’ DSS, that’s why i’m a little rusty. Thanks anyways for adding those methods I didn’t mention.

I don’t like it saving every minute but I use saving when the player leaves and bind to close just in case so players don’t lose data in my opinion that is the best way to get about to it.

1 Like

I’m saying no because you are mentioning the usage of saving when the value is changed.

No, the autosave is not needed, however it will prevent data loss if the PlayerRemoving and/or BindToClose fails.

1 Like

Are the ones in red just their inventory? Or is it marked true/false on whether or not it’s unlocked?