Advertisement
UMAYEERIBN

Datastore Script By: UndefinedN00B/ITestForUMAYEERIBN

May 11th, 2020
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.44 KB | None | 0 0
  1. --// Scripted By: UndefinedN00B / ITestForUMAYEERIBN \\--
  2.  
  3. local dataStores = game:GetService("DataStoreService"):GetDataStore("CashDS") -- Getting DS
  4. local harDS = game:GetService("DataStoreService"):GetDataStore("HarvestDS")
  5. local backDS = game:GetService("DataStoreService"):GetDataStore("backDS")
  6. local InventoryDS = game:GetService("DataStoreService"):GetDataStore("InventoryDS")
  7. local axDS = game:GetService("DataStoreService"):GetDataStore("AxeDS")
  8. local gDS = game:GetService("DataStoreService"):GetDataStore("GunDS")
  9. local defuldlevel = 0 -- Getting default values
  10. local playersLeft = 0
  11. local bases = workspace:WaitForChild("Bases")
  12.  
  13.  
  14.  
  15. game.Players.PlayerAdded:Connect(function(player) -- When player is added
  16.    
  17.     playersLeft = playersLeft + 1 -- add value +1
  18.  
  19.     -- create a leaderstats folder
  20.     local leaderstats = Instance.new("Folder")
  21.     leaderstats.Name = "leaderstats"
  22.     leaderstats.Parent = player
  23.  
  24.     -- create a values for the leaderstats
  25.     local cash = Instance.new("IntValue")
  26.     cash.Name = "Cash"
  27.     cash.Value = 0
  28.     cash.Parent = leaderstats
  29.     local harvest = Instance.new("IntValue")
  30.     harvest.Name = "Harvested"
  31.     harvest.Value = 0
  32.     harvest.Parent = leaderstats
  33.    
  34.     local plrData = Instance.new("Folder")
  35.     plrData.Name = player.Name
  36.     plrData.Parent = game.ServerStorage:WaitForChild("PlrData")
  37.    
  38.     local invent = Instance.new("Folder")
  39.     invent.Name = "Inventory"
  40.     invent.Parent = player
  41.    
  42.     local oax = Instance.new("BoolValue")
  43.     oax.Name = "OwnsAxe"
  44.     oax.Parent = player
  45.     local ogu = Instance.new("BoolValue")
  46.     ogu.Name = "OwnsGun"
  47.     ogu.Parent = player
  48.    
  49.     local equippedVal = Instance.new("IntValue")
  50.     equippedVal.Name = "BackEquipped"
  51.     equippedVal.Value = 20
  52.     equippedVal.Parent = player
  53.    
  54.     local currentStore = Instance.new("IntValue")
  55.     currentStore.Name = "CurrentStore"
  56.     currentStore.Parent = player
  57.     local moneyGet = Instance.new("IntValue")
  58.     moneyGet.Name = "MoneyGet"
  59.     moneyGet.Parent = player
  60.     local deb = Instance.new("BoolValue")
  61.     deb.Name = "PlayerDebounce"
  62.     deb.Parent = player
  63.    
  64.     local infarm = Instance.new("BoolValue")
  65.     infarm.Name = "InFarm"
  66.     infarm.Parent = player
  67.    
  68.     -- make nil variables for data
  69.     local player_data
  70.     local harves_data
  71.     local back_data
  72.     local invent_data
  73.     local equipped_data
  74.     local axe_data
  75.     local gun_data
  76.    
  77.     -- get the data from the datatsores
  78.     pcall(function()
  79.         player_data = dataStores:GetAsync(player.UserId.."-cash")
  80.     end)
  81.    
  82.     pcall(function()
  83.         harves_data = harDS:GetAsync(player.UserId.."-harvested")
  84.     end)
  85.    
  86.     pcall(function()
  87.         back_data = backDS:GetAsync(player.UserId.."-backpacks")
  88.     end)
  89.    
  90.     pcall(function()
  91.         invent_data = InventoryDS:GetAsync(player.UserId.."-inventory")
  92.     end)
  93.    
  94.     pcall(function()
  95.         equipped_data = backDS:GetAsync(player.UserId.."-equipped")
  96.     end)
  97.    
  98.     pcall(function()
  99.         axe_data= axDS:GetAsync(player.UserId.."-axe")
  100.     end)  
  101.    
  102.     pcall(function()
  103.         gun_data= gDS:GetAsync(player.UserId.."-gun")
  104.     end)  
  105.    
  106.     -- if the data is true, set it
  107.  
  108.     if player_data ~= nil then
  109.         cash.Value = player_data
  110.         print("Player Cash Loaded In.")
  111.     else
  112.         cash.Value = defuldlevel
  113.     end
  114.    
  115.     if axe_data ~= nil then
  116.         oax.Value = axe_data
  117.         print("Player Axe Loaded.")
  118.     else
  119.         oax.Value = false
  120.         print("Player Doesn't own axe")
  121.     end
  122.    
  123.     if gun_data ~= nil then
  124.         ogu.Value = gun_data
  125.         print("Player Gun Loaded.")
  126.     else
  127.         ogu.Value = false
  128.         print("Player Doesn't own axe")
  129.     end
  130.    
  131.    
  132.     if harves_data ~= nil then
  133.         harvest.Value = harves_data
  134.         print("Player Harvest Loaded In.")
  135.     else
  136.         harvest.Value = 0
  137.     end
  138.    
  139.     -- loop through data and find the tool. Then duplicate it and put it in Player.plrData
  140.     if back_data then
  141.         for _, v in pairs(back_data) do
  142.             if game.ServerStorage:WaitForChild("BackVals"):FindFirstChild(v) then
  143.                 local clonedV = game.ServerStorage.BackVals[v]:Clone()
  144.                 clonedV.Parent = plrData
  145.                 print(v.." has been loaded to "..player.Name)
  146.             end
  147.         end
  148.        
  149.         if equipped_data then
  150.             equippedVal.Value = equipped_data
  151.         end
  152.     else
  153.         print("No backpack data")
  154.     end
  155.    
  156.     if invent_data then
  157.         for _, v in pairs(invent_data) do
  158.             if game.ServerStorage:WaitForChild("InventoryOb"):FindFirstChild(v) then
  159.                 local newInv = game.ServerStorage.InventoryOb[v]:Clone()
  160.                 newInv.Parent = invent
  161.                 print(v.." Has been loaded in "..player.Name.."'s Inventory.")
  162.             end
  163.         end
  164.     else
  165.         print("Nothing in Inventory")
  166.     end
  167.    
  168.    
  169.    
  170.     if oax.Value ~= false then
  171.         if not player:WaitForChild("Backpack"):FindFirstChild("Axe") then
  172.             local axe = game.ServerStorage:WaitForChild("PlrBackpack"):WaitForChild("Axe"):Clone()
  173.             axe.Parent = player.Backpack
  174.         end
  175.     end
  176.    
  177.     deb.Value = false
  178.    
  179. end)
  180.  
  181. local bindableEvent = Instance.new("BindableEvent") -- create a bindable event
  182.  
  183. game.Players.PlayerRemoving:Connect(function(player)
  184.    
  185.     -- save the data with a pcall just in case some error comes up
  186.     pcall(function()
  187.         dataStores:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
  188.         print("Cash Has been save for "..player.Name)
  189.     end)
  190.    
  191.     pcall(function()
  192.         axDS:SetAsync(player.UserId.."-axe", player.OwnsAxe.Value)
  193.         print("Axe Has Been Saved For "..player.Name)
  194.     end)
  195.    
  196.     pcall(function()
  197.         gDS:SetAsync(player.UserId.."-gun", player.OwnsGun.Value)
  198.         print("Axe Gun Been Saved For "..player.Name)
  199.     end)
  200.    
  201.     -- loop through each item and put it in table. then save the table
  202.     pcall(function()
  203.         local ownedBacks = game.ServerStorage.PlrData[player.Name]:GetChildren()
  204.         local backTable = {}
  205.        
  206.         for _, v in pairs(ownedBacks) do
  207.             table.insert(backTable, v.Name)
  208.         end
  209.        
  210.         backDS:SetAsync(player.UserId.."-backpacks", backTable)
  211.        
  212.         if player:WaitForChild("BackEquipped").Value ~= nil then
  213.             local equippedSaVal = player.BackEquipped
  214.            
  215.             backDS:SetAsync(player.UserId.."-equipped", equippedSaVal.Value)
  216.         end
  217.        
  218.         print("Backpacks have been saved for "..player.Name)
  219.     end)
  220.    
  221.     pcall(function()
  222.         local stuff = player:WaitForChild("Inventory"):GetChildren()
  223.         local inventTable = {}
  224.        
  225.         for _, v in pairs(stuff) do
  226.             table.insert(inventTable, v.Name)
  227.         end
  228.        
  229.         InventoryDS:SetAsync(player.UserId.."-inventory", inventTable)
  230.         print(player.Name.."'s inventory has been saved.")
  231.     end)
  232.    
  233.     pcall(function()
  234.         harDS:SetAsync(player.UserId.."-harvested", player.leaderstats.Harvested.Value)
  235.         print("Player Harvest Saved")
  236.     end)
  237.    
  238.    
  239.     playersLeft = playersLeft - 1
  240.     bindableEvent:Fire()
  241. end)
  242.  
  243. -- when all the players leave make sure to wait till data is saved, then shutdown the server
  244. game:BindToClose(function()
  245.     while playersLeft > 0 do
  246.         bindableEvent.Event:Wait()
  247.     end
  248. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement