Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DataStore = {}
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local RunService = game:GetService("RunService")
- local DataService = game:GetService("DataStoreService")
- local CURRENT_KEY = "Testing5"
- DataStore.Shared = require(script.Parent.Shared)
- DataStore.DataChanged = game:GetService("ReplicatedStorage").DataChanged
- DataStore.DataInitialized = false
- DataStore.BaseStats = {
- Health = 100,
- Stamina = 100,
- RegenDelay = 10,
- WalkSpeed = 12,
- RunSpeed = 20,
- Stance = "Forward",
- State = "Idle",
- Race = "Human",
- Look = true,
- Freeze = false,
- Slowed = false,
- Blocking = false,
- Disabled = false,
- FreshSpawn = true,
- }
- DataStore.SavedStats = {
- "Health",
- "Stamina",
- "Race",
- "Keybinds"
- }
- DataStore.Data = {
- ["IdleDummy"] = {
- State = "Idle",
- Blocking = false,
- Stamina = 100,
- },
- ["ForwardGuardDummy"] = {
- State = "Combat",
- Stance = "Forward",
- Blocking = true,
- Stamina = 100,
- },
- ["LeftGuardDummy"] = {
- State = "Combat",
- Stance = "Left",
- Blocking = true,
- Stamina = 40,
- },
- ["RightGuardDummy"] = {
- State = "Combat",
- Stance = "Right",
- Blocking = true,
- Stamina = 100,
- },
- ["BackGuardDummy"] = {
- State = "Combat",
- Stance = "Back",
- Blocking = true,
- Stamina = 100,
- }
- }
- function DataStore:GetData(Player, Stat)
- return self.Data[Player.Name][Stat]
- end
- function DataStore:SetData(Player, Stat, NewValue)
- local Args = {}
- Args[Stat] = NewValue
- self.Data[Player.Name][Stat] = NewValue
- if RunService:IsServer() then self:ReplicateData(Player, Args) else self.DataChanged:Fire(Stat, NewValue) end
- end
- function DataStore:ReplicateData(Player, Args)
- self.Shared["ClientServer"]:FireClient(Player, "DataHandler", "Replicate", Args)
- end
- function DataStore:SaveData(Player)
- local Data_Store = DataService:GetDataStore(CURRENT_KEY)
- local ToSave = {}
- for Stat, Value in pairs(self.Data[Player.Name]) do
- if table.find(self.SavedStats, Stat) then
- ToSave[Stat] = Value
- end
- end
- local Success, Error = pcall(function()
- Data_Store:SetAsync(Player.Name.."Data", ToSave)
- end)
- if not Success then print("Data failed to save: "..Error) else print("Data saved.") end
- end
- function DataStore:GrabData(Player)
- local Data_Store = DataService:GetDataStore(CURRENT_KEY)
- local SavedData
- local Success, Error = pcall(function()
- SavedData = Data_Store:GetAsync(Player.Name.."Data")
- end)
- if not SavedData then self:InitData(Player) return end
- self.Data[Player.Name] = self["BaseStats"]
- for StatName, StatValue in pairs(SavedData) do
- self.Data[Player.Name][StatName] = StatValue
- end
- self:ReplicateData(Player, self.Data[Player.Name])
- end
- function DataStore:InitData(Player, Dummy)
- self.Data[Player.Name] = self["BaseStats"]
- if Dummy then return end
- self:ReplicateData(Player, self.Data[Player.Name])
- end
- return DataStore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement