Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I use DataStoreService?

I have a leaderboard for a player's coins, but how would I make it save by using Roblox's DataStoreService?

1 answer

✓ Accepted answer

Saving data is a great way to keep people playing your game! It uses ROBLOX's DataStoreService to save a players data when they leave, and get it back when they will join!

Let's start with making a variable for the service, such as we might do for Replicated Storage!

local datastoreservice = game:GetService("DataStoreService")

Now that we have a variable to represent our service, we need another to create a DataStore, using the service. We have this feature so that developers can have multiple stores (Currency store, XP store, ETC).

We do this by using the "GetDataStore" method.

local datastore = datastoreservice:GetDataStore("DataStore") -- you may change this to whatever purpose you are using the datastore for "CoinStore", "XPStore", ETC.

Feel free to change "DataStore" to whatever your need is for the store ("CoinStore", "XPStore", ETC).

Now that we have a DataStore, we need to learn how to use them! Remember, all data stores have a key, and a value (the key is the player's UserId, and the value is the amount you want to store like coins). Do use these we will use Asyncs. Mainly "GetAsync(key)" which returns the value, and "UpdateAsync(key, value)" which saves the key and it's value.

Here is an example data store!

local datastoreservice = game:GetService("DataStoreService") -- identifies the service
local datastore = datastoreservice:GetDataStore("CoinStore") -- creates the DataStore called for a player's coins.

game.Players.PlayerAdded:Connect(function(plr) -- a new player just joined
	local leaderstats  = Instance.new("Folder", ) -- creates the folder to place the data
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats" -- names the folder as leaderstats so it can go on ROBLOX's default player list.
	local coins = Instance.new("IntValue") -- creates a value (for the player's currency)
	coins.Parent = leaderstats
	coins.Name = "Coins" -- names the value as coins
	coins.Value = datastore:GetAsync(plr.UserId) or 0 -- if there is no value for player because they started playing the game, it will be a nil value so we will set the default value to 0
end)

game.Players.PlayerRemoving:Connect(function(leavingplr) -- a player is leaving
	datastore:UpdateAsync(plr.UserId, plr.leaderstats.Coins.Value) -- saves the amount of coins they have
end)

If you need more help, contact me or check ROBLOX's official page on this service!

10 comments

0

When explaining DataStores it is always important to at least introduce UpdateAsync -- it is just as important as SetAsync and GetAsync. Also, changing a DataStore name doesn't wipe out its data, it changes the key. If you change from DS1 to DS2, DS1 still contains the old data.

0

True... I'll fix it.

0

Intance.new("class", parent) is deprecated. use local item = Instance.new("class") item.Parent = ...

0

it is!?

0

Ok, I am ACTUALLY triggered at roblox. There was NO need to depreciate it in the FIRST place, it saved developers a lot of time! Since when was it depreciated!?

0

It is not deprecated.

0

It's been deprecated for years now. It causes performance issues.

0

Oh, interesting. Never known.

0

:( WHY ROBLOX?

This discussion is preserved as it was. New answers, comments, and votes are closed.