Advertisement
VoltixDevelopment

Datastore Service Example

Jul 12th, 2024
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.39 KB | None | 0 0
  1. local dataStoreService=game:GetService("DataStoreService") -- Getting/ Requiring the Service and it's Data Model.
  2. -- This allows us to use it's Methods :GetDataStore(), :GetOrderedDataStore(), GetRequestBudgetForRequestType()
  3. local dataStore=dataStoreService:GetDataStore("PlayerData") -- Asking the dataStoreService for this Table of contents.
  4. -- If the above Table of contents doesn't yet exist one will be created with the name provided, PlayerData.
  5. -- This table has Functions already preset by the functions of the Data Store Service Model.
  6. -- Methods for this table: dataStore:GetAsync(), dataStore:SetAsync(), dataStore:RemoveAsync(),
  7. -- dataStore:UpdateAsync(), dataStore:IncrementAsync()
  8. --local SD=game:GetService("ReplicatedStorage"):WaitForChild("Remotes").SaveData -- I've set up a RemoteEvent Instance in this path.
  9. -- You don't need this but it could be helpful if you wanted to allow the player to manually save their data.
  10.  
  11. game:GetService("Players").PlayerAdded:Connect(function(player) -- Whenever a player joins the game we'll do this...
  12.  
  13.     local leaderstats=Instance.new("Folder") -- Creating an Instance with the Class Folder.
  14.     leaderstats.Name="leaderstats" -- For any numerical values store within it to be shown on the CoreGUI Leaderboard you need to
  15.     -- Set the name to exactly, "leaderstats".
  16.     -- If you have a custom GUI that displays this information then the name can be anything. Using leaderstats will..
  17.     -- only be useful if you want the values of the Instances below to be shown on the default leaderstats
  18.     leaderstats.Parent=player -- Setting the Folder Instances parent to the Player.
  19.  
  20.     local Coins=Instance.new("IntValue") -- Creating an Instance with the Class IntValue.
  21.     Coins.Name="Coins" -- name it anything, Cash, cash, Money, money, Moneyz, BigMan420, doesn't matter.
  22.     Coins.Parent=leaderstats -- Putting this IntValue inside the Folder we made above for the player.
  23.  
  24.     local CoinData -- Just a temporary holder for any Values returned by the PCall Below.
  25.     local success, errormessage=pcall(function() -- PCalls or Protected Calls allow us to call another function/ Method...
  26.     -- Without the risk of crashing, creating errors without our scripts main function, etc.
  27.     -- Basically this just means that if the function errors for any reason we can handle is 'gracefully'
  28.     -- Meaning we know the error occured and was expecting it so now we can make/ execute our fallback plan.
  29.     -- Which I setup a tiny bit of Error handling down below without first encountering an error, I used error prevention.
  30.     -- Of course if it does for some odd reason fail, it will print the reason for failing as we state below.
  31.     -- and yes, you can stack multiple PCalls within a single one if you really wanted or needed to...
  32.         CoinData=dataStore:GetAsync(player.UserId.."-Coins") -- This is the :GetAsync() function/ Method I mentioned...
  33.         -- At the start of the script. This allows us to call a function predetermined by Roblox that..
  34.         -- will get data from a table, the table is the one we created called PlayerData.
  35.         -- This table is stored within the DataStoreService and can get accessed by the :GetDataStore()
  36.         -- Method/ Function I listed at the start of the script.
  37.         -- The :GetAsync() Function requires a Key to index the value of. ~To get the value of the key, provide the key~
  38.         -- Our Keys should be unique since every players data will be saved within this table.
  39.         -- My key is the Players User ID plus -Coins so.. (1234567890-Coins)
  40.         -- Until we set the Value of the Key then it will be equal to nothing or nil.
  41.         -- We can use any Value Type we want, we could even have it return a function if we needed too...
  42.         -- For now we're using normal ole IntValues or Numerical Values.
  43.     end)
  44.  
  45.     if success and CoinData~=nil then -- If the Protected/ PCall function from above was successful at returning the data we requested...
  46.     -- and the Value isn't equal to nothing at all then we'll set the leaderstats object(s) we created earlier to represent the value...
  47.     -- Returned by the PCall/ Protected Function, A.K.A. The CoinData, that local we made to temporarily store the data...
  48.     -- It's now useful like so...
  49.         warn("The player "..player.Name.." has gotten the coins value data using the key: "..tostring(player.UserId).."-Coins")
  50.         warn("Key = Value")
  51.         warn("The Key used in the table: "..tostring(player.UserId).."-Coins")
  52.         warn("The Value of the Key: "..tostring(CoinData))
  53.     -- Ignore the warnings I made above this was also to help out but they can be removed.
  54.         Coins.Value=CoinData -- This is where we modify that IntValue we created earlier...
  55.         -- We're changing it's Value to the CoinData or Data returned by the PCall using our Temporary local/ placeholder.
  56.     elseif CoinData==nil then -- Error Prevention, Preventing it from doing nothing if it's nil.
  57.         warn("This player doesn't have a value stored on the key: "..tostring(player.UserId).."-Coins")
  58.         local success, errormessage=pcall(function() -- Another PCall to protect the PCall it's inside of...
  59.             dataStore:SetAsync(player.UserId.."-Coins",0) -- Setting a value that isn't nil...
  60.         end)
  61.         if success then
  62.             warn("We've set a numerical value of zero to hold the place of nil instead.")
  63.             Coins.Value=CoinData -- If we was able to succesfully set the Value of the Key we passed then we'll...
  64.             -- Set the IntValue on the leaderstats to this number.
  65.         else
  66.             warn(errormessage) -- If the SetAsync Fails it'll warn the error specific message now.
  67.         end
  68.     else
  69.         warn(errormessage) -- If the GetAsync Fails it'll warn the error specific message now.
  70.     end
  71. end)
  72.  
  73. game:GetService("Players").PlayerRemoving:Connect(function(plr)
  74. -- Same as PlayerAdded just for whenever a player is leaving...
  75. -- Same information applies here...
  76.    
  77.     local success, errormessage=pcall(function()
  78.         dataStore:SetAsync(plr.UserId.."-Coins",plr:WaitForChild("leaderstats"):FindFirstChild("Coins").Value)
  79.     end)
  80.  
  81.     if success then
  82.         warn(plr.Name.." has left and their Coins data was saved! ")
  83.         warn("Key = Value")
  84.         warn("The Key used in the table: "..tostring(plr.UserId).."-Coins")
  85.         warn("The New Value of the Key: "..tostring(plr:WaitForChild("leaderstats"):FindFirstChild("Coins").Value))
  86.     else
  87.         warn(errormessage)
  88.     end
  89. end)
  90.  
  91. --[[SD.OnServerEvent:Connect(function(Plr)
  92.    
  93.     local success, errormessage=pcall(function()
  94.         dataStore:SetAsync(Plr.UserId.."-RoleplayName",Plr.leaderstats.Coins.Value)
  95.     end)
  96.  
  97.     if success then
  98.  
  99.     else
  100.         warn(errormessage)
  101.     end
  102. end)]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement