Hi, I’m currently making a data store system using OOP. I want to detect changes in a nested table (or the player data)
Module script:
local PlrData = {}
PlrData.__index = PlrData
local transferData = require(script.Parent.transferData)
local function printTable(tab, string)
for i,v in pairs(tab) do
if type(v) == "table" then
print(string,i," : ",v,":::")
printTable(v, string.." ")
else
print(string,i," : ",v)
end
end
end
function PlrData:newData() -- Create new data if the player is new
local newData = {
PlrStats = {
Strength = 1,
Agility = 1,
Magic = 1,
Vitality = 1,
Defense = 1,
},
}
return newData
end
local SavingFor = {}
function PlrData.init(plr)
local plrTable = {}
local self = setmetatable({}, PlrData)
self.dataStore = game:GetService("DataStoreService"):GetDataStore("test")
self.key = plr.UserId .. "'s Data"
self.table = plrTable
local mt = setmetatable(PlrData, {
__index = function(t, k)
if type(self[k]) == "table" then
-- if I change the strength of the player, it would not print out anything because it doesnt detect any changes to self.table
end
return self[k]
end,
__newindex = function(t, k, v)
self[k] = v
return self[k]
end,
})
return mt
end
function PlrData:GetData(plr) -- return the data table of the according plr
return self.table[plr.UserId]
end
function PlrData:LoadData(plr) -- load the data of the according player
local Data
local suc, err = pcall(function()
Data = self.dataStore:GetAsync(self.key)
end)
if not suc then
return warn(err)
end
self.table[plr.UserId] = Data or self:newData()
end
function PlrData:SaveData(plr)
if not SavingFor[plr] then
SavingFor[plr] = true
local plrId = plr.UserId
if self.table[plrId] then
local suc, err = pcall(function()
self.dataStore:SetAsync(self.key, self.table[plrId])
end)
if not suc then
return warn("Data wasn't saved for " .. plr.Name .. ".")
end
end
SavingFor[plr] = nil
end
end
function PlrData:UpdateData(plr, newDataTbl)
self.table[plr.UserId] = newDataTbl
printTable(self.table[plr.UserId], "")
end
return PlrData
So basically I want to detect any changes made inside this table
plrID = {
PlrStats = {
Strength = 1,
Agility = 1,
Magic = 1,
Vitality = 1,
Defense = 1,
},
}
While returning local PlrData = {} PlrData.__index = PlrData
. So far I have only made it to detect changes to the first hierarchy of the PlrData
. I want it to detect changes to the stats in the self.table
itself, like whenever I change the Strength
, etc.
I have looked at other posts but the code seems very confusing and hard to implement into OOP
Any help is appreciated!