How would I save data?

Hello, developers. I am currently working on a game where you can earn money. However, I don’t know how I would save players’ money amount, instead of players having to re-earn their money every time they play.

Screenshot (29076)

Would I buy the datastore plugin? Or do I need to script? Please let me know, thanks. :slight_smile:

1 Like

Use datastores, I mean unless you want to use a custom database I don’t see the problem, unless if your genuinely confused and don’t know what datastores are.

You can read this tutorial on datastores here.

1 Like

I know that datastores store data, etc., I’m just not sure how to make one since I’m still pretty new to programming.

Many thanks, I’ll try that. :slight_smile:

Datastores have two main functions setting and getting, think of a datastore as a huge table.

The Key is basically the index of the table.

And to get the value of this key you do

Datastore:SetAsync(KEYNAME, VALUE)

This basically maps a key in that database (huge table) to a value provided.

DataStore:GetAsync(Key), gets a value from the key provided. If there is no key named this it makes a new key mapped to this value.

just think of datastores as huge tables. I mean do you know what tables are?

Yes I’m pretty sure I know what tables are, I’ll try that.

Many thank yous! :slight_smile:

here would be an example.

DataStore:SetAsync(“JohhnyCoins”, 200) – Sets the value of the key, since there is no key in the database assuming this is the first time you ran this script, it creates a new key named JohhnyKey set to 200.

local Data = DataStore:GetAsync(“JohhnyCoins”) – gets the value of the Johnnycoins key in the database

print(Data) – would print 200

Worth noting that UpdateAsync is far more useful and relevant than SetAsync. It’s almost always going to be more useful.

@QuackingDuckQuack You really ought to learn about data stores properly through the article @Coldshot_Dev linked (Data Stores | Documentation - Roblox Creator Hub). Please let me know if you find any room for improvement on the article, and post it in #platform-feedback:developer-hub, we’re always looking for feedback on our introductory articles.

1 Like

This wasn’t meant as a full tutorial. Obviously i’m not going to teach everything, It was meant for the basic concepts. I mean if OP asks I could teach about the other things to.

I heavily recommend knowing how to script (Roblox Lua) before you attempt to try datastore. You should know the basics before you try datastore.

If you’re new, make sure to know all the basics.

I do know quite a few things about Roblox like printing, cframes, children, parents, descendants, etc. It’s just I’ve never used datastore before.

Focus on Lua rather than API. Functions, loops, pcalls, tables, globals, global functions (e.g. math, string global functions) and more.

Just to let ya know that the datastore plugin isn’t needed for saving data. It’s meant to browse and edit data in DataStores. :slightly_smiling_face:

There are a few ways you could save data for a player:

  1. Use DataStoreService
  2. Use a DataStoreService wrapper (e.g. ProfileService/DataStore2)
  3. Host it on an external database such as MySQL/MongoDB/etc.

If you’re completely new to the concept of saving/loading data, you should use DataStoreService as it’s a good start.

If you want to take it a small step further, you can use a DataStore wrapper that lets you save data without any worry of data loss (as mentioned earlier, DataStore2 is a good start for those new to using a DataStore wrapper).

Or, if you really want to take it further, use an external database that is either
A. hosted on the cloud
B. hosted yourself

You’d have to create an external REST API that is hosted by yourself/or on the cloud that saves and load data to the external database, and to communicate with that API you’ve just created, you’d have to use HttpService.

These are a few ways that you could save data from the top of my head.

Lots of thank yous, I’ll try that. :slight_smile:

Still new to datastores. :laughing:

1 Like

Saving data can be daunting at first, but the fundamentals are pretty simple.

Create data store/key - > Get key when player joins the game / load data inside the key - > save data to key on player leave.

SAMPLE CODE
local Players = game:GetService("Players") 
local DataStoreService = game:GetService("DataStoreService") 
local PlayerDataStore = DataStoreService("PlayerDataStore") -- can be named whatever you wish

Players.PlayerAdded:Connect(function(player)

    local stats = Instance.new("Folder", player) -- creating a folder inside the player for ease of access in other scripts. 
    stats.Name = "Stats" 

    local coins = Instance.new("IntValue", player) -- these values can be anything, I just decided that coins would be a practical fit. 
    coins.Name = "Coins"

    local gems = Instance.new("IntValue", player) 
    gems.Name = "Gems" 

    local data = nil

    local success, err = pcall(function() -- wrapped in a pcall because data stores have a tendency to error, the pcall returns a boolean and a string with the error 
        data = PlayerDataStore:GetAsync(player.UserId.."-data") -- the key created can be anything, but it's best practice to use the players user id which never changes, concated with a string
    end) 

    if success then -- if grabbing the data was successful

        if data then -- it was successful but there might not be data, it could've been the first time a player joined
            coins.Value = data.Coins -- accessing keys we created inside of the data dictionary 
            gems.Value = data.Gems
        else -- this means that it was the player's first time joining the game, successful but no data was found. 
            coins.Value = 0 -- other default values can be set
            gems.Value = 0
        end
    else -- if it wasn't successful in grabbing the data
        warn("Unable to grab data!") -- a special type of print that outputs in orange text. 
        warn("Error:", err) 
    end


end)

Players.PlayerRemoving:Connect(function(player)

    local coins = player:WaitForChild("Stats"):WaitForChild("Coins")
    local gems = player:WaitForChild("Stats"):WaitForChild("Gems")

    local data = { -- creating a new dictionary to store data that needs to be saved
    Coins = coins.Value; -- adding keys which can be accessed later when loading data
    Gems = gems.Value;
    } 

    local success, err = pcall(function() -- wrapped in a pcall as these have a tendency to error. 
        PlayerDataStore:UpdateAsync(player.UserId.."-data", function() -- most guide use :SetAsync() however :UpdateAsync is the canonical way to save data and it's the best practice. 
            return data -- saving the dictionary to be accessed in the loading function 
        end)
    end) 

    if success then -- if saving was successful 
        print("Data saved successfully!") 
    else -- saving wasn't successful, warn the error
        warn("Unable to save player's data... ") 
        warn("Error:", err) 
    end


end)
1 Like

If you’re new to data stores, I highly recommend this beginner’s tutorial by @Alvin_Blox (teaches how to save currency using the DataStoreService):

He explains all of the key information quite well throughout the video.

2 Likes

As all of the replies have said, your problem can be fixed easily with the DataStoreService. If you search up datastore roblox on YouTube, you should find many tutorials that will teach you very well.

1 Like