So I am currently working on a system right, so I have a table, and in the table is a metatable called “Value”. When you do X.Value you can access any value that is in another table that I have, for instance,
X.Value.Money -- gives you, well the money
So next I did setting values! Yippe, this worked very easily,
Now my problem comes in when I try to set the value of a table, for instance
X.Value.Parties[1] = false
now sense Parties is not in fact a metatable, and the __newindex is attached to X.Value, it doesnt get triggered when I change anything in Parties. So what do I do?
My wonderful code:
-- // Services //
local runService = game:GetService('RunService')
-- // Values //
-- || Module ||
local module = {}
local values = {}
-- // Functions //
local function loopThrough(Item:Instance)
local tab = {}
for key,value in Item:GetAttributes() do
if tonumber(key) then
tab[tonumber(key)] = value
else
tab[key] = value
end
end
for _, child in Item:GetChildren() do
if tonumber(child.Name) then
tab[tonumber(child.Name)] = loopThrough(child)
else
tab[child.Name] = loopThrough(child)
end
end
return tab
end
-- // Main //
-- || Module Functions ||
local function Format()
return loopThrough(script)
end
-- // Main //
-- || Value ||
local Value = {}
Value.__index = function(Table,Key)
local formated = Format()
return formated[Key]
end
Value.__newindex = function(Table, key, set) -- Set
if script:GetAttribute(key) ~= nil then
script:SetAttribute(key,set)
end
end
setmetatable(values,Value)
-- || Self ||
local self = {}
self.Value = values
self.Format = Format
return table.freeze(self)