Advertisement
VapeL

Gamer DataStore

Sep 25th, 2020
2,222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.66 KB | None | 0 0
  1. --// Script Variables \\--
  2.  
  3. local Players = game:GetService('Players')
  4. local DataStore = game:GetService('DataStoreService')
  5. local RunService = game:GetService('RunService')
  6.  
  7. local playerData = DataStore:GetDataStore('Gamerν”Œ')
  8.  
  9. --// Settings \\--
  10.  
  11. local StarterData = {
  12.     Skill1 = 'One';
  13.     Skill2 = 'Two';
  14.     Skill3 = 'Three';
  15.     Skill4 = 'Four';
  16.     Skill5 = 'Five';
  17.     Skill6 = 'E';
  18.     Skill7 = 'R';
  19.     Skill8 = 'C';
  20.     Skill9 = 'V';
  21.     Sheathe = 'Q'; --Sheath/Unsheath weapon
  22.     Inventory = 'I'; --Inventory
  23.     Settings = 'O'; --Options/Settings
  24.     Friends = 'U'; --Online friends, and the world they're in
  25.     skillTree = 'K'; --Skill tree
  26.     Sprint = 'LeftControl';  --Sprint
  27.     Block = 'F';
  28.     Trees = true;
  29.     Level = 1
  30. };
  31.  
  32. local savetable = {};
  33. local AUTO_SAVE_INTERVAL = 180;
  34.  
  35. --// Functions \\--
  36.  
  37. --/ Load Starter Data \--
  38.  
  39. local function loadStarterData(Player)
  40.    
  41.     local Key = "ν”Œ_"..Player.UserId
  42.    
  43.     local Hotkeys = Instance.new('Folder')
  44.     Hotkeys.Name = "Keybinds"
  45.     Hotkeys.Parent = Player
  46.     local Options = Instance.new('Folder')
  47.     Options.Name = "Options"
  48.     Options.Parent = Player
  49.     local Stats = Instance.new('Folder')
  50.     Stats.Name = "Stats"
  51.     Stats.Parent = Player
  52.     local Inventory = Instance.new('Folder')
  53.     Inventory.Name = "Inventory"
  54.     Inventory.Parent= Player
  55.     for i, v in pairs(StarterData) do
  56.         if typeof(v) == 'string' then
  57.             local strValue = Instance.new('StringValue')
  58.             strValue.Parent = Hotkeys
  59.             strValue.Name = i
  60.             strValue.Value = v
  61.         elseif typeof(v) == 'boolean' then
  62.             local boolValue = Instance.new('BoolValue')
  63.             boolValue.Parent = Options
  64.             boolValue.Name = i
  65.             boolValue.Value = v
  66.         elseif typeof(v) == 'number' then
  67.             local intValue = Instance.new('IntValue')
  68.             intValue.Parent = Stats
  69.             intValue.Name = i
  70.             intValue.Value = v
  71.         end
  72.     end
  73.     print(Player.Name..'\'s data has been created..')
  74. end
  75.  
  76. --/ Load Data \--
  77.  
  78. local function loadData(Player)
  79.     local Key = "ν”Œ_"..Player.UserId
  80.    
  81.     local Data
  82.    
  83.     local success, err = pcall(function()
  84.         Data = playerData:GetAsync(Key)
  85.     end)
  86.    
  87.     if success then
  88.         print('Lmao this fucking works what how')
  89.     elseif err then
  90.         print('Lmao this was expected')
  91.     end
  92.    
  93.     if Data then
  94.         for i,v in pairs(Data) do
  95.             if typeof(v) == 'string' then
  96.                 Player.Hotkeys[i].Value = v
  97.             elseif typeof(v) == 'boolean' then
  98.                 Player.Options[i].Value = v
  99.             elseif typeof(v) == 'number' then
  100.                 Player.Stats[i].Value = v
  101.             end
  102.         end
  103.         print(Player.Name..'\'s data has been loaded!')
  104.     else
  105.         print(Player.Name..' has no data. Generated new data!')
  106.     end
  107. end
  108.  
  109. --/ Save Data \--
  110.  
  111. local function saveData(Player)
  112.     local Key = "ν”Œ_"..Player.UserId
  113.    
  114.     local Data = {};
  115.    
  116.     for _, v in pairs(Player.Keybinds:GetChildren()) do
  117.         Data[v.Name] = v.Value
  118.     end
  119.    
  120.     for _, v in pairs(Player.Keybinds:GetChildren()) do
  121.         Data[v.Name] = v.Value
  122.     end
  123.    
  124.     for _, v in pairs(Player.Keybinds:GetChildren()) do
  125.         Data[v.Name] = v.Value
  126.     end
  127.    
  128.     local function UpdateAsyncFunction(oldValue)
  129.         local previousData = oldValue or {DataId = 0}
  130.         if Data.DataId == previousData.DataId then
  131.             Data.DataId = Data.DataId + 1
  132.             return Data
  133.         else
  134.             return nil
  135.         end
  136.     end
  137.    
  138.     local success,err = pcall(function()
  139.         if playerData == nil then
  140.             playerData:SetAsync(Key, Data)
  141.             print('Set data!')
  142.         else
  143.             playerData:UpdateAsync(Key, UpdateAsyncFunction())
  144.             print('Updated data!')
  145.         end
  146.     end)
  147.    
  148.     if success then
  149.         print('Lmao this fucking works what how')
  150.     elseif err then
  151.         print('Lmao this was expected')
  152.     end
  153. end
  154.  
  155. Players.PlayerAdded:Connect(function(Player)
  156.     loadStarterData(Player)
  157.     loadData(Player)
  158. end)
  159.  
  160. Players.PlayerRemoving:Connect(function(Player)
  161.     saveData(Player)
  162. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement