mc3334

DataStore Sample script

May 29th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.81 KB | None | 0 0
  1. DS = game:GetService("DataStoreService"):GetDataStore("Money")
  2. GoldStartingValue = --Put how much gold you want a player to start with here!
  3.  
  4. game.Players.PlayerAdded:Connect(function(plr)
  5.     local ls = Instance.new("Folder")
  6.     ls.Name = "leaderstats"
  7.     ls.Parent = plr
  8.     local VAL = Instance.new("IntValue")
  9.     VAL.Name = "Gold"
  10.     VAL.Parent = ls
  11.     local data
  12.     local succ, err = pcall(function()
  13.         data = DS:GetAsync(plr.UserId)
  14.     end)
  15.     if err then
  16.         warn("Failed to load data for "..plr.Name)
  17.     end
  18.     if not data then
  19.         data = GoldStartingValue
  20.     end
  21.     VAL.Value = data
  22. end)
  23.  
  24. game.Players.PlayerRemoving:Connect(function(plr)
  25.     local data = plr.leaderstats.Gold.Value
  26.     local succ, err = pcall(function()
  27.         DS:SetAsync(plr.UserId,data)
  28.     end)
  29.     if err then
  30.         warn("Data failed to save for "..plr.Name)
  31.     end
  32. end)
Advertisement
Add Comment
Please, Sign In to add comment