PheonixStudio

Datastore Script #1

Apr 11th, 2020
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.33 KB | None | 0 0
  1. --// Services
  2. local ds = game:GetService("DataStoreService"):GetDataStore("TutorialData") -- Replace TutorialData with your Datstore Name.
  3.  
  4.  
  5. --// Functions
  6. game.Players.PlayerAdded:Connect(function(plr) -- On player enter
  7.     local Prefix = plr.UserId -- Quick access to the Users ID.
  8.    
  9.     local leaderstats = Instance.new("Folder", plr) -- Creates a new folder for the Cash to go in. the 2nd parameter is for the Parent.
  10.     leaderstats.Name = "leaderstats" -- Names it
  11.    
  12.     local Cash = Instance.new("IntValue", leaderstats) -- Creates a IntValue for Cash.
  13.     Cash.Name = "Cash" -- Names it
  14.    
  15.     local CashData = nil -- To grab the data of the value we'll store the value into this variable.
  16.    
  17.     pcall(function()
  18.         CashData = ds:GetAsync(Prefix) -- Sets the Variable to the Value saved or makes it nil for a new player.
  19.     end)
  20.        
  21.     if CashData ~= nil then -- If the user does have data then we can load it into the Cash.
  22.         Cash.Value = CashData
  23.     else
  24.         Cash.Value = 0 -- Else if CashData == nil then creates a new amount for the player. aka as the Start value
  25.     end
  26. end)
  27.  
  28. game.Players.PlayerRemoving:Connect(function(plr) -- On player leave
  29.     local Prefix = plr.UserId -- Quick access to the Users ID.
  30.    
  31.     local Cash = plr.leaderstats.Cash -- Locates the Players cash.
  32.    
  33.     pcall(function()
  34.         ds:SetAsync(Prefix, Cash.Value) -- Saves it.
  35.     end)
  36. end)
Advertisement
Add Comment
Please, Sign In to add comment