-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS2 Wrapper.lua
61 lines (46 loc) · 1.51 KB
/
DS2 Wrapper.lua
1
2
3
4
5
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local DS2Wrapper = {}
DS2Wrapper.__index = DS2Wrapper
--[[
DataStore2 Wrapper by frriend
Allows automatic saving, loading, verification, and updating of Values within a folder!
API:
:Register(PLAYER_FOLDER, PLAYER)
--]]
local DS2 = require(game.ReplicatedStorage.DataStore2) -- Path to Datastore 2
DS2.Combine("PlayerData", "Data")
function DS2Wrapper:Register(PlayerDataFolder, Player)
task.spawn(function()
local DefaultDataFolder = PlayerDataFolder:Clone()
-- Create DataStore for Player --
local PlayerDataStore = DS2("Data", Player)
-- Create a Default Dictionary to Compare To --
local DefaultData = {}
for _, item in ipairs(DefaultDataFolder:GetDescendants()) do
DefaultData[tostring(item.Name)] = item.Value
end
-- Get Player Data | If it doesn't exist, set it to the defaults --
local PlayerData = PlayerDataStore:Get(DefaultData)
-- Verify Player Data --
for key, value in pairs(DefaultData) do
if PlayerData[key] == nil then
PlayerData[key] = value
end
end
-- Remove Depricated Data --
for key, _ in pairs(PlayerData) do
if DefaultData[key] == nil then
PlayerData[key] = nil
end
end
-- Update/Listen to Player Cache --
for _, valueObject in ipairs(PlayerDataFolder:GetDescendants()) do
valueObject.Value = PlayerData[valueObject.Name]
valueObject.Changed:Connect(function(value)
PlayerData[valueObject.Name] = value
PlayerDataStore:Set(PlayerData)
end)
end
DefaultDataFolder = nil
end)
end
return DS2Wrapper