-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS2 Utilities.lua
More file actions
147 lines (105 loc) · 3.44 KB
/
DS2 Utilities.lua
File metadata and controls
147 lines (105 loc) · 3.44 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
local module = {}
local dataFolder = script.Parent
local DataStore2 = require(dataFolder:WaitForChild"DataStore2")
function module.createFolder(parent, name)
local folder = Instance.new("Folder", parent)
folder.Name = name
return folder
end
function module.packADataTableFolder(dataTableFolder)
local packedDataTable = {}
for _, obj in pairs(dataTableFolder:GetChildren())do
local objectData = {
obj.ClassName,
obj.Name,
obj.Value,
obj:GetAttributes()
}
print("attr: ", obj:GetAttributes())
table.insert(packedDataTable, objectData)
end
return packedDataTable
end
--[[
KeysData Order:
key || value's parent name || value's name || value's Class Name || starter Value || Value's MinValue || Value's MaxValue
Min and Max Value only used if the Value's ClassName is DoubleConstrainedValue
]]
--// Data creation
function module.createData(key, player, folder, name, className, starterValue, minValue, maxValue)
local value
if starterValue then
value = Instance.new(className)
local parent = player:FindFirstChild(folder)
if not parent then
parent = Instance.new("Folder", player)
parent.Name = folder
end
value.Parent = parent
value.Name = name
if className == "DoubleConstrainedValue"then
value.MinValue = minValue
value.MaxValue = maxValue
end
--| Data Saving Updating
local valueStore = DataStore2(key, player)
-- updates the value and if the player is new it'll give him the starterValue
value.Value = valueStore:Get(starterValue)
-- Updates the value when the data value gets changed
--valueStore:OnUpdate(function(newValue)
-- value.Value = newValue
--end)
-- Updates the data value when the value gets changed
value:GetPropertyChangedSignal"Value":Connect(function()
valueStore:Set(value.Value)
end)
else
value = Instance.new(className)
local parent = player:FindFirstChild(folder)
if not parent then
parent = Instance.new("Folder", player)
parent.Name = folder
end
value.Parent = parent
value.Name = name
end
return value
end
--// Data Table Creation:
function module.createDataTable(tableKey, player, tableName, defaultDataTable)
local dataTable
local dataTableFolder
dataTableFolder = Instance.new("Folder", player)
dataTableFolder.Name = tableName
-- Data table loading
local dataTableStore = DataStore2(tableKey, player)
-- updates the table and if the player is new it'll give him the defaultDataTable
dataTable = dataTableStore:Get(defaultDataTable)
-- Updates the data table when anything in the table gets changed
local function updateDataTable()
dataTableStore:Set(module.packADataTableFolder(dataTableFolder))
end
-- Loops through table's data
for _, data in pairs(dataTable) do
local dataObject = Instance.new(data[1], dataTableFolder)
dataObject.Name = data[2]
dataObject.Value = data[3]
-- Creating data attributes
if data[4]then
for attributeName, attributeValue in pairs(data[4])do
dataObject:SetAttribute(attributeName, attributeValue)
end
end
dataObject:GetPropertyChangedSignal("Name"):Connect(updateDataTable)
dataObject.Changed:Connect(updateDataTable)
dataObject.AttributeChanged:Connect(updateDataTable)
end
-- Updates the value when the data value gets changed
--valueStore:OnUpdate(function(newValue)
-- value.Value = newValue
--end)
dataTableFolder.DescendantRemoving:Connect(updateDataTable)
dataTableFolder.DescendantAdded:Connect(updateDataTable)
return dataTableFolder
end
return module