How to detect change value in my table

local Template = require(script.Parent.Template)

local Data = {}
Data.__index = Data

function Data.new()
	local self = setmetatable({}, Data)
	
	self.Template = Template
	
	return self
end

function Data:setValue(valueName, value)
	local findValue = self.Template[valueName]
	
	if findValue then
		findValue += value
	end
end

function Data:subValue(valueName, value)
	local findValue = self.Template[valueName]

	if findValue then
		findValue -= value
	end
end

function Data:mulValue(valueName, value)
	local findValue = self.Template[valueName]

	if findValue then
		findValue *= value
	end
end

function Data:divValue(valueName, value)
	local findValue = self.Template[valueName]

	if findValue then
		findValue /= value
	end
end

function Data:getTemplate()
	return self.Template
end

function Data:remove()
	table.clear(self)
	setmetatable(self, nil)
end

return Data

thanks for help !

i edited… this title x) (ps: for you view my post x) )

Do you want to check when self.Template is changed?

yeahhh true with the methamethode __newindex

Use the metamethod __newindex. Here is an example taken from a post on how it would work:

--[[
	We will be indexing surfaceTable the whole time,
	which will stay empty.
]]
local surfaceTable = {}
--[[
	Proxy table is going to be hidden, but will actually reflect
	all the changes. This table will be updated each time.
]]
local _mainTable = {}

setmetatable(surfaceTable, {
	__index = function(self,key)
		print("Key number ".. key .." was accessed.")
		return _mainTable[key] -- We return the key from proxy table.
	end,
	
	__newindex = function(self,key,value)
		print("Key number ".. key .. " was updated. New value: ".. (value or "nil"))
		_mainTable[key] = value -- Now we update our proxy table.
	end,
})

Here’s the replacement for table.insert and table.remove (__newindex does not work with these methods in the Lua version Luau is based of).

local tab = {}
table.insert(tab, "element")
-- Equivalent operation:
tab[#tab+1] = "element"
local tab = {"element"}
table.remove(tab, 1)
-- Equivalent operation:
tab[1] = nil

All the information is extracted from this post, check it out for more information:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.