Advertisement
TaylorsRus

DataStore

Mar 9th, 2023
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. local DataStore = {}
  2.  
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4. local RunService = game:GetService("RunService")
  5. local DataService = game:GetService("DataStoreService")
  6.  
  7. local CURRENT_KEY = "Testing5"
  8.  
  9. DataStore.Shared = require(script.Parent.Shared)
  10.  
  11. DataStore.DataChanged = game:GetService("ReplicatedStorage").DataChanged
  12.  
  13. DataStore.DataInitialized = false
  14.  
  15. DataStore.BaseStats = {
  16.     Health = 100,
  17.     Stamina = 100,
  18.     RegenDelay = 10,
  19.  
  20.     WalkSpeed = 12,
  21.     RunSpeed = 20,
  22.    
  23.     Stance = "Forward",
  24.     State = "Idle",
  25.     Race = "Human",
  26.    
  27.     Look = true,
  28.     Freeze = false,
  29.     Slowed = false,
  30.     Blocking = false,
  31.     Disabled = false,
  32.    
  33.     FreshSpawn = true,
  34. }
  35.  
  36. DataStore.SavedStats = {
  37.     "Health",
  38.     "Stamina",
  39.     "Race",
  40.     "Keybinds"
  41. }
  42.  
  43. DataStore.Data = {
  44.     ["IdleDummy"] = {
  45.         State = "Idle",
  46.         Blocking = false,
  47.         Stamina = 100,
  48.     },
  49.     ["ForwardGuardDummy"] = {
  50.         State = "Combat",
  51.         Stance = "Forward",
  52.         Blocking = true,
  53.         Stamina = 100,
  54.     },
  55.     ["LeftGuardDummy"] = {
  56.         State = "Combat",
  57.         Stance = "Left",
  58.         Blocking = true,
  59.         Stamina = 40,
  60.     },
  61.     ["RightGuardDummy"] = {
  62.         State = "Combat",
  63.         Stance = "Right",
  64.         Blocking = true,
  65.         Stamina = 100,
  66.     }, 
  67.     ["BackGuardDummy"] = {
  68.         State = "Combat",
  69.         Stance = "Back",
  70.         Blocking = true,
  71.         Stamina = 100,
  72.     }
  73. }
  74.  
  75. function DataStore:GetData(Player, Stat)
  76.     return self.Data[Player.Name][Stat]
  77. end
  78.  
  79. function DataStore:SetData(Player, Stat, NewValue)
  80.     local Args = {}
  81.     Args[Stat] = NewValue
  82.    
  83.     self.Data[Player.Name][Stat] = NewValue    
  84.    
  85.     if RunService:IsServer() then self:ReplicateData(Player, Args) else self.DataChanged:Fire(Stat, NewValue) end
  86. end
  87.  
  88. function DataStore:ReplicateData(Player, Args) 
  89.     self.Shared["ClientServer"]:FireClient(Player, "DataHandler", "Replicate", Args)
  90. end
  91.  
  92. function DataStore:SaveData(Player)
  93.     local Data_Store = DataService:GetDataStore(CURRENT_KEY)
  94.    
  95.     local ToSave = {}
  96.    
  97.     for Stat, Value in pairs(self.Data[Player.Name]) do
  98.         if table.find(self.SavedStats, Stat) then
  99.             ToSave[Stat] = Value
  100.         end
  101.     end
  102.    
  103.     local Success, Error = pcall(function()
  104.         Data_Store:SetAsync(Player.Name.."Data", ToSave)
  105.     end)
  106.    
  107.     if not Success then print("Data failed to save: "..Error) else print("Data saved.") end
  108. end
  109.  
  110. function DataStore:GrabData(Player)
  111.     local Data_Store = DataService:GetDataStore(CURRENT_KEY)
  112.    
  113.     local SavedData
  114.     local Success, Error = pcall(function()
  115.         SavedData = Data_Store:GetAsync(Player.Name.."Data")
  116.     end)
  117.    
  118.     if not SavedData then self:InitData(Player) return end
  119.    
  120.     self.Data[Player.Name] = self["BaseStats"]
  121.     for StatName, StatValue in pairs(SavedData) do
  122.         self.Data[Player.Name][StatName] = StatValue
  123.     end
  124.    
  125.     self:ReplicateData(Player, self.Data[Player.Name])
  126. end
  127.  
  128. function DataStore:InitData(Player, Dummy)
  129.     self.Data[Player.Name] = self["BaseStats"]
  130.     if Dummy then  return end
  131.    
  132.     self:ReplicateData(Player, self.Data[Player.Name])
  133. end
  134.  
  135. return DataStore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement