I'm learning datastore and I'm confused on this? What do they do? How do I use them? Another thing is. When do I use ds = game:GetService("DataStore")?
DataStore is a new feature that some developers are starting to get into. They can save a simple string or number value and can be used server to server without a player even ever having to enter the server to open the data.
:GetDataStore(String)
, even though you didn't ask I thought this would be important to go over, you're basically setting up or using a like subsection of DataStore, that way you can have like a Kills Data Store and a WipeOuts DataStore
:SetAsync(String, Value)
will save the text or number value. Under the Name of the value and the value itself.
ds = game:GetService("DataStore"):GetDataStore("Test") ds:SetAsync("KOs_" .. tostring(player.userId), player.Kills.Value) --The code would save Knock out by userId
:GetAsync(String)
will load up the value set from SetAsync.
ds = game:GetService("DataStore"):GetDataStore("Test") if ds:GetAsync("KOs_" .. tostring(player.userId)) ~= nil then player.Kills.Value = ds:GetAsync("KOs_" .. tostring(player.userId)) else player.Kills.Value = 0 end --The code will see if previous data exists, if it does then the data store loads it, if not then the value is 0.
These are the basics I have learned, and probably be the basics I will live with. If you have any questions I might help if you just leave a comment, or consult this page from the wiki.
Oh, SetAsync()
is to save Data in DataStoreService and GetAsync()
is to Load Data.
Calling DataStore(Defining):
Let's say I where to save a person data when they leave.
ds = game:GetService("DataStoreService") datastore ds:GetDataStore("PlrData") game.Players.PlayerRemoving:connect(function(plr) z = plr:WaitForChild("leaderboard") x = z:GetChildren() for i = 1, #x do datastore:SetAsync(x[i].Name,x[i].Value) end end)
Hope this helped
+1 id this helped :)