Advertisement
Quoteory

Untitled

Jan 2nd, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | None | 0 0
  1. players.PlayerAdded:Connect(function(player) -- when a player joins this all
  2.     local character = player.Character or player.CharacterAdded:Wait()
  3.     local creditStore = DataStore2("credit", player) -- setting up money save
  4.     local toolsStore = DataStore2("Tools", player) -- setting up tool save
  5.  
  6.  
  7.     local function updateClientCredits(amount) -- updates credits
  8.         replicatedstorage.ClientCreditUpdate:FireClient(player, amount) -- updates the credit UI
  9.     end    
  10.  
  11.     updateClientCredits(creditStore:Get(defaultCreditAmount))  -- if theres no data for credits it will set to the default amount
  12.     creditStore:OnUpdate(updateClientCredits) -- whenever the credit number is changed it updates
  13.  
  14.    local function CharacterAdded(char)
  15.         local toolsStore = DataStore2("Tools", player)
  16.         local Backpack = player.Backpack
  17.         local toolData = nil
  18.         local Ready = false
  19.  
  20.         if Ready == false then
  21.             Ready = true
  22.  
  23.             toolData = toolsStore:Get(nil)
  24.  
  25.             if toolData then
  26.                 toolGive(player, toolData) -- fires the function to actually give the tools
  27.                 Edit(player, toolData)
  28.             end
  29.         end
  30.        
  31.         --[[ this code doesn't belong here, you're calling the function from itself
  32.         CharacterAdded(character)
  33.         player.CharacterAdded:Connect(CharacterAdded)
  34.         --]]
  35.  
  36.         local count = 0
  37.    
  38.         local function Adjust() -- adds stuff to the list of tools that save
  39.  
  40.             local list = {}
  41.             local toolsStore = DataStore2("Tools", player)
  42.             local equippedTool = char:FindFirstChildOfClass("Tool")
  43.             if equippedTool then
  44.                 table.insert(list, equippedTool.Name)
  45.                 toolsStore:Set(list)
  46.  
  47.             end
  48.  
  49.             local Tools = Backpack:GetChildren()
  50.             for i = 1, #Tools do
  51.                 table.insert(list, Tools[i].Name)
  52.                 toolsStore:Set(list)
  53.             end
  54.  
  55.             if count ~= #list then
  56.                 Edit(player, toolData)
  57.                 count = #list
  58.             end
  59.         end
  60.         Backpack.ChildAdded:connect(Adjust)
  61.         Backpack.ChildRemoved:connect(Adjust)
  62.  
  63.         char.ChildAdded:connect(function(child) -- Whenever something is added it will add stuff to the list of saving
  64.             if child.ClassName == "Tool" then
  65.                 Adjust()
  66.             end
  67.         end)
  68.  
  69.         char.ChildRemoved:connect(function(child)-- Whenever something is added it will remove stuff to the list of saving
  70.             if child.ClassName == "Tool" then
  71.                 Adjust()
  72.             end
  73.         end)
  74.  
  75.     end -- char added function end
  76.  
  77.     -- code belongs here since it's in the playeradded block
  78.     CharacterAdded(character)
  79.     player.CharacterAdded:Connect(CharacterAdded)
  80. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement