Advertisement
EmeraldLimes

DS2Handler Source Code

Nov 13th, 2020 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.61 KB | None | 0 0
  1.  --[[
  2.     Made by EmeraldLimes on 14th of Nov. 2020
  3.     Devforum post on how to use: https://devforum.roblox.com/t/ds2handler-an-easier-simpler-way-to-use-ds2-open-source/869823
  4.  ]]
  5. local Names,Defaults = {
  6.    
  7. },{}
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. local DS2Handler = {}
  34. local DS2 = require(script:WaitForChild("DataStore2"))
  35.  
  36. function init()
  37.     for _,combineTable in pairs(Defaults) do
  38.         local key, names = combineTable.combineKey, {}
  39.         for name,default in pairs(combineTable) do
  40.             if (name ~= "combineKey") then
  41.                 Defaults[name] = default
  42.                 table.insert(names,name)
  43.             end
  44.         end
  45.         DS2.Combine(key,table.unpack(names))
  46.     end
  47. end
  48. init()
  49.  
  50. function DS2Handler.__call(_,func,DS2Name,plr,...)
  51.     if (not func or not DS2Name or not plr) then warn("["..(script.Name).."]","Invalid arguments") return end
  52.    
  53.     local args = table.pack(...)
  54.     local dataStore = DS2(DS2Name,plr)
  55.    
  56.     local success,result = pcall(function()
  57.         if (func == "Get") then
  58.             return dataStore:Get(Defaults[DS2Name])
  59.         elseif (func == "Increment") then
  60.             local incrementValue = table.unpack(args)
  61.             dataStore:Increment(incrementValue,Defaults[DS2Name])
  62.         elseif (func == "Set") then
  63.             local setTo = table.unpack(args)
  64.             dataStore:Set(setTo)
  65.         elseif (func == "GetTable") then
  66.             return dataStore:GetTable(Defaults[DS2Name])
  67.         elseif (func == "OnUpdate") then
  68.             local callback = table.unpack(args)
  69.             dataStore:OnUpdate(callback)
  70.             callback(dataStore:Get(Defaults[DS2Name]))
  71.         end
  72.     end)
  73.    
  74.     if (success) then
  75.         return result
  76.     else
  77.         warn("[DS2Handler] "..result)
  78.     end
  79. end
  80.  
  81. return setmetatable(DS2Handler,DS2Handler)
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement