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.
Would I buy the datastore plugin? Or do I need to script? Please let me know, thanks.
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.
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
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.
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.
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.
There are a few ways you could save data for a player:
Use DataStoreService
Use a DataStoreService wrapper (e.g. ProfileService/DataStore2)
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.
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)
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.