Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I use data store?

Asked by
yoshiegg6 176
9 years ago

Can anyone explain data store to me? I've read the wiki but it's confusing. I know I'll get downvotes for not putting a script but I don't know where to start.

2 answers

Log in to vote
2
Answered by
yumtaste 476 Moderation Voter
9 years ago

DataStores are a better version of Data Persistence. Basically, DataStores can be used to, well, store data. Why would you store data, you ask? You might want to save progress in a game, save stats for leaderboards, etc. Anything you can imagine. You don't have unlimited space, 45000 units of data to be exact, but it's much more than you'll ever need.

EDIT: He meant how would someone use it in a script. DON'T JUDGE THE ANSWER UNTIL I REMOVE THE FOLLOWING NOTE:

What note? This answer is done!

local dataStore = game:GetService("DataStoreService"):GetDataStore("insertNameHere") --gets the DataStore, there's no function to create one, so you can just replace "insertNameHere" with any name and it will create the DataStore if it doesn't already exist.

game.Players.PlayerAdded:connect(function(player) --this, of course, can be any event. this script just tracks how many times a player has joined the game
dataStore:IncrementAsync(player.Name.." joins", 1) --increases the "(player name) joins" Async value by 1.
print(player.Name.." has joined the game "..dataStore:GetAsync(player.Name.." joins")
end)

Basically, Asyncs are like file folders in a file cabinet. The file cabinet is the data store, and the paper inside the folders is data. So, you could rename the DataStore to "playerJoinTracker," but when you change the name of a DataStore, you will lose all the data. However, you could create a script that transfers all the data. If you need me to post an example script like that, I can. Please upvote and accept!

EDIT: Yoshiegg kindly requested that I give an example of how to transfer data. No problem.

Warning: This part is kind of difficult and I may make errors in my script. Please disregard the code until I or someone else has confirmed that the code works, as I don't want to put your game's data in jeopardy.

There's not really a way that I could think of where you could just make a script that transfers data, rather, you would have to change the script that saves the data.

local storeService = game:GetService("DataStoreService")
local oldDataStore = storeService:GetDataStore("testjoins")
local newDataStore = storeService:GetDataStore("newjoins")
local transferredData = storeService:GetDataStore("didTransferData")

game.Players.PlayerAdded:connect(function(player)
local transferrableData = oldDataStore:GetAsync(player.Name.. " joins1")
newDataStore:UpdateAsync(player.Name.." joins2", transferrableData) --transfers old data into new store
newDataStore:IncrementAsync(player.Name.." joins2", 1) --adds 1 value for join so it still works
end)

I have yet to add a conditional statement. Don't use the code yet.

0
I know but how would you use it in a script? yoshiegg6 176 — 9y
0
Ohhh...I'll put that in. yumtaste 476 — 9y
0
Thank you. Still kind of hard but you explained it well. yoshiegg6 176 — 9y
0
Also, please post the example of transfering the data. yoshiegg6 176 — 9y
View all comments (2 more)
0
Ok, no problem. yumtaste 476 — 9y
0
I owe you one. yoshiegg6 176 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

You should definitely check out the wiki, they've got some great stuff on DataStores. DataStoreService DataStore DataStore Tutorial

0
Whilst this answer is helpful, it's not really answering the question. He said "I've read the Wiki but it's confusing". If you did want to make sure that he knew about each of those articles, you should post the links as a comment instead. duckwit 1404 — 9y

Answer this question