generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Data Store Tutorial

Previous Thread :: Next Thread 
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
15 Mar 2014 05:01 PM
Ok, so plenty of people have came here and asked for a tutorial on data Store, so I decided to create one for you guys.


So lets start off with calling Data Store
just do:

local datastore = game:GetService("DataStoreService")

it doesn't have to be datastore, it could be what ever you want. This gives us access to the data store.

But that isn't all we need to add. We also need to tell it what "section" of data store to look for

so do

local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

It does not need to be PlayerStats, but for this tutorial, we are going to do that.


now we have access to a section in datastore we choose. when you call the :GetDataStore() method, it will automatically create a "section" to hold your saved items inside it, If you haven't already.

Now for this tutorial we are going to create a script that will save and load the leaderstats of a player when they join a game.

So first we need an event for when a player leaves a game.


local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerRemoving:connect(function(player)

end)

there is our first event. But of course, we need to add to it in order to do anything. Assuming you have a leaderstat script, we are going to find it inside of the player.


local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats = player:FindFirstChild("leaderstats")
end)



Now we have access to the leaderstats Item inside the player. If you do not have a leaderstat script in workspace, this will error.
now we need to acess all the stats inside of leaderstats


local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats = player:FindFirstChild("leaderstats"):GetChildren()

for i = 1, #stats do -- creates a loop for all the stats
print(stats[i].Name")
end
end)

this will simply print() the name of each stat we are saving. Now we need to save the stats. We will use the SetAsync() method.


local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats = player:FindFirstChild("leaderstats"):GetChildren()

for i = 1, #stats do -- creates a loop for all the stats
print(stats[i].Name")
datastore:SetAsync(stats[i].Name, stats[i].Value)
end
end)


now we have saved all the stats in the leaderstats section. When we call :SetAsync, we need to tell it which data store to save it to, the name of the datastore item we wish to use, and the value of the datastore item. "stats[i].Name" would be the name of the stat from leaderstats. stats[i].Value is the value of the stat we wish to save to the data store item.

Now we need to load the data store.
first, we need an event to tell when a player joins the game.


local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats = player:FindFirstChild("leaderstats"):GetChildren()

for i = 1, #stats do -- creates a loop for all the stats
print(stats[i].Name")
datastore:SetAsync(stats[i].Name, stats[i].Value)
end
end)

-- Loading dem stats

game.Players.PlayerAdded:connect(function(newplayer)

end)



now we once again need to access the stats of the player in order to change them again.



local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats = player:FindFirstChild("leaderstats"):GetChildren()

for i = 1, #stats do -- creates a loop for all the stats
print(stats[i].Name")
datastore:SetAsync(stats[i].Name, stats[i].Value)
end
end)

-- Loading dem stats
game.Players.PlayerAdded:connect(function(newplayer)
newplayer:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats2 = newplayer:FindFirstChild("leaderstats"):GetChildren()
end)



now we need to change the stats value to the saved data store we earlier. We will use the :GetAsync() method.




local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats = player:FindFirstChild("leaderstats"):GetChildren()

for i = 1, #stats do -- creates a loop for all the stats
print(stats[i].Name")
datastore:SetAsync(stats[i].Name, stats[i].Value)
end
end)

-- Loading dem stats
game.Players.PlayerAdded:connect(function(newplayer)
newplayer:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats2 = newplayer:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #stats2 do
stats2[i].Value = datastore:GetAsync(stats2[i].Name)
end
end)

GetAsync will retrieve a DataStore from the called datastore "section". It returns the value we saved.


Atlast we are done! We have a fully working script to save a stat value as a data store and load it as a value again. If you have any questions, comments, or concerns. Please tell me below in the comments section.
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
15 Mar 2014 05:02 PM
Took me half an hour to write this. I feel great.

Aaand now I don't. All thanks to floodcheck.
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
15 Mar 2014 05:26 PM
bump

u want 2 lern
Report Abuse
ROBLOXTHEMOVIEs is not online. ROBLOXTHEMOVIEs
Joined: 11 Jun 2012
Total Posts: 5895
15 Mar 2014 05:39 PM
What's the diffrence between data store and data persistence.

I won a math debate
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
15 Mar 2014 05:45 PM
Great question.

First of all,
the pros.

Data store can be shared between two or more places as long as they are in the same 'game'(read about that here: http://wiki.roblox.com/index.php?title=Games).

Data store is much more reliable than data persistance. Saved data will not be lost when a server shuts down, when it may with data persistance.

There is an ordered data store which sorts Int values from greatest to least.

the cons

Data store cannot save Instances like scripts, parts, or anything created by Instance.new()

possible things I may not know
Report Abuse
ROBLOXTHEMOVIEs is not online. ROBLOXTHEMOVIEs
Joined: 11 Jun 2012
Total Posts: 5895
15 Mar 2014 05:46 PM
Wow.
I'm switching to Data Store :P
Thanks for the help!

I won a math debate
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
15 Mar 2014 05:50 PM
Thats what SH is here for. Help.
Report Abuse
ben1034655 is not online. ben1034655
Joined: 08 Jul 2009
Total Posts: 55
15 Mar 2014 05:58 PM
Data Store will really help with my new game that will be conected with other 2 places :D Thanks night! c:
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
15 Mar 2014 06:09 PM
No prob benny.
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
15 Mar 2014 09:03 PM
Bring
Up
My
Post
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
16 Mar 2014 12:40 AM
Bumping again :3
Report Abuse
UFAIL2 is not online. UFAIL2
Joined: 14 Aug 2010
Total Posts: 6905
16 Mar 2014 07:31 AM
Correct me if I am wrong, but you are saving the name of the key the same for every player, thus rendering this a waste. If you like to write tutorial's make sure you know enough information to do the task at hand.
Report Abuse
rangersmash is not online. rangersmash
Joined: 18 Nov 2009
Total Posts: 2891
16 Mar 2014 07:38 AM
You made mistakes about printing, and errors in the code. Check before you post :/
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
16 Mar 2014 09:32 PM
Sorry about the code errors, this was 100% typed in the new thread window.

Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
16 Mar 2014 09:33 PM
Oh, I see what I xid wrong. Whoops.
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
16 Mar 2014 09:40 PM
I created a similar acript to this, no errors, a while ago for my sister tecara. You are free to use it or see the real code.




But hey, at least you guys learned datastore, I just forgot the logic in the example.

http://www.roblox.com/DataStore-Save-Leaderboard-item?id=149414714
Report Abuse
firebed101 is not online. firebed101
Joined: 24 May 2008
Total Posts: 110
14 Apr 2014 10:16 AM
Thank you! It may have taken you at least a half hour to write this, probably a lot longer, but I've spent a month searching for a helpful Datastorage tutorial and this enlightened me in like 5 minutes. Oh and you may want to fix the part where it says:

local stats = player:FindFirstChild("leaderstats"):GetChildren()

for i = 1, #stats do -- creates a loop for all the stats
print(stats[i].Name") --<---- it won't print! Grrr! >:|
datastore:SetAsync(stats[i].Name, stats[i].Value)
end
end)

-- Loading dem stats

With the print error aside, does this it work across "game universes?"
Report Abuse
Bebee2 is not online. Bebee2
Joined: 17 May 2009
Total Posts: 3985
14 Apr 2014 10:55 AM
Note:

Data Store + RPG + Universes = Horribad mix


Data store values are not immediately the value you just set.
Report Abuse
KiwiTronik is not online. KiwiTronik
Joined: 18 Feb 2010
Total Posts: 3221
14 Apr 2014 07:28 PM
TRYING TO SAVE STRING STATS:
local ds = game:GetService("DataStoreService"):GetDataStore("Inventory")
stats={"Slot1","Slot2","Slot3","Slot4","Slot5","Slot6","Slot7","Slot8","Slot9","Slot10","Slot11","Slot12"}--change these to whatever stats you want
game.Players.PlayerAdded:connect(function(plyr)
plyr:WaitForDataReady()
local a=Instance.new("StringValue")
a.Parent=plyr
a.Name="Inventory"
for i=1,#stats do
local stat=Instance.new("StringValue")
stat.Parent=a

stat.Name=stats[i]
local b = tonumber(stat.Name:sub(5))
if b >= 5 then
stat.Value = "Locked"
else
stat.Value="Empty"
end

end
local child2=plyr.Inventory:GetChildren()
for i=1, #child2 do
child2[i].Value=ds:GetAsync(child2[i].Name)--(plyr.userId..child[i].Name)
end




end)

game.Players.PlayerRemoving:connect(function(plyr)
plyr:WaitForDataReady()
wait()
local child=plyr.Inventory:GetChildren()
for i=1, #child do
ds:SetAsync(child[i].Name, child[i].Value)
end
end)


PPLEASE HELP!
Report Abuse
ayub32 is not online. ayub32
Joined: 27 Dec 2009
Total Posts: 485
07 Jun 2014 04:58 PM
Deserves bump
Report Abuse
maxomega3 is not online. maxomega3
Joined: 11 Jun 2010
Total Posts: 10668
07 Jun 2014 05:01 PM
A couple of notes:

PlayerRemoving is a horrible way to save a value. Try saving values when they change.

you forgot a con:
Data_P is EASIER
Report Abuse
Forefend is not online. Forefend
Joined: 22 Mar 2013
Total Posts: 2
17 Jun 2014 03:37 PM
Very helpful!
Report Abuse
BlueMond is not online. BlueMond
Joined: 26 Dec 2007
Total Posts: 47
20 Jun 2014 02:52 PM
I know that you can set the scope of a data store but most don't just like you didn't because the default is global but how would you set the scope to local? I have a place with places branching off of it and I want them to access a data store known as "Permissions" but I don't want them to all access the same data store globally because the permissions for each places are different.
Report Abuse
BlueMond is not online. BlueMond
Joined: 26 Dec 2007
Total Posts: 47
23 Jun 2014 09:59 AM
Nevermind that last post. I figured out I can just get the placeID from the datamodel and use that for the namespace of the scope to ensure it uses the right permissions datastore.
Report Abuse
nighttimeninja314 is not online. nighttimeninja314
Joined: 04 Apr 2011
Total Posts: 4001
23 Jun 2014 10:00 AM
Good.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image