Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local module = {}
- module.DataStoreName = "Data1"
- module.Save_LoadInStudio = true
- module.DataStore = game:GetService("DataStoreService"):GetDataStore(module.DataStoreName)
- if game.Players.LocalPlayer then warn("DataStore requested on the client, is a server-only module.") return end
- function module.Load_Table_Data(Player,ObjectsToLoad)
- if type(Player) ~= "userdata" then error("Bad argument #1 to LoadData (player object expected) got "..type(Player)) return end
- if type(ObjectsToLoad) ~= "table" then error("Bad argument #2 to LoadData (Table expected) Got "..type(ObjectsToLoad)) return end
- local tries = 0
- local s
- local e
- local data
- repeat
- tries = tries + 1
- s, e = pcall(function()
- data = module.DataStore:GetAsync(Player.UserId)
- end)
- until tries == 3 or s
- if e and module.Save_LoadInStudio then
- warn("data did not load! module.Load_Table_Data make sure you have Studio Access to API Services on")
- return
- end
- if data then
- for i , v in pairs(ObjectsToLoad) do
- v.Value = data[v.Name]
- end
- end
- end
- function module.Load_Value(Player,object)
- if type(Player) ~= "userdata" then error("Bad argument #1 to LoadData (player object expected) got "..type(Player)) return end
- if type(object) ~= "userdata" then error("Bad argument #1 to LoadData (Value object expected) got "..type(object)) return end
- local tries = 0
- local s
- local e
- local data
- repeat
- tries = tries + 1
- s, e = pcall(function()
- data = module.DataStore:GetAsync(Player.UserId)
- end)
- until tries == 3 or s
- if data then
- object.Value = data
- end
- if e and module.Save_LoadInStudio then
- warn("data did not load! module.Load_Value make sure you have Studio Access to API Services on")
- return
- end
- end
- function module.Save_Table_Data(Player,ObjectsToSave)
- if type(Player) ~= "userdata" then error("Bad argument #1 to LoadData (player object expected) got "..type(Player)) return end
- if type(ObjectsToSave) ~= "table" then error("Bad argument #2 to LoadData (Table expected) Got "..type(ObjectsToSave)) return end
- local tries = 0
- local s
- local e
- repeat
- tries = tries + 1
- s , e = pcall(function()
- module.DataStore:UpdateAsync(Player.UserId,function(data)
- data={}
- for i ,v in pairs(ObjectsToSave) do
- data[v.Name] = v.Value
- end
- return data
- end)
- end)
- until tries == 3 or s
- if e and module.Save_LoadInStudio then
- warn("data did not save! module.Save_Table_Data make sure you have Studio Access to API Services on")
- return
- end
- end
- function module.Save_Value_Data(Player,object)
- if type(Player) ~= "userdata" then error("Bad argument #1 to LoadData (player object expected) got "..type(Player)) return end
- if type(object) ~= "userdata" then error("Bad argument #1 to LoadData (Value object expected) got "..type(object)) return end
- local tries = 0
- local s
- local e
- repeat
- tries = tries + 1
- s , e = pcall(function()
- module.DataStore:UpdateAsync(Player.UserId,function(data)
- data = object.Value
- return data
- end)
- end)
- until tries == 3 or s
- if e and module.Save_LoadInStudio then
- warn("data did not save! module.Save_Value_Data make sure you have Studio Access to API Services on")
- return
- end
- end
- return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement