Edit: just realized what you wanted. Use the __newindex on a metatable. Ex:
Metatable = {}
Metatable.__newindex = function(selfTable,key,value)
if key == "Enabled" then
rawset(selfTable,"Enabled",value)
-- do stuff with the value variable
print("set to " .. tostring(value))
end
end
setmetatable({},Metatable).Enabled = false -- "set to false"
Who is listening to the change? If it’s a client then fire the client, which I’m assuming will be found through the Owner data. If it’s the server, then a BindableEvent would be useful. Just remember that only one subscriber can be listening at a time with the Bindable Event.
I am getting some weird stack overflow errors. This is what I tried to do
local Gun = {}
Gun.__index = Gun
Gun.__newindex = function(metaData,key,value)
if key == "Enabled" then
metaData.Enabled = value
print("test", metaData.Enabled)
-- do stuff with the value variable
end
end
function Gun.new(gunInstance, owner, enabled)
local metaData = setmetatable({}, Gun)
metaData.Model = gunInstance
metaData.Owner = owner
metaData.Enabled = enabled
return metaData
end
return Gun
rawset is needed as if I didn’t use it, it would constantly set the Enabled variable again, making an infinite loop. Though now that I think about it, a side effect of this would be that it wouldn’t work again if you set it again so maybe you should have a separate table for the actual values.
local Gun = {}
Gun.__index = Gun
Gun.__newindex = function(metaData,key,value)
if key == "Enabled" then
rawset(metaData,"Enabled",value)
print("test", metaData.Enabled)
-- do stuff with the value variable
end
end
function Gun.new(gunInstance, owner, enabled)
local metaData = setmetatable({}, Gun)
metaData.Model = gunInstance
metaData.Owner = owner
metaData.Enabled = enabled
return metaData
end
return Gun
local Gun = {}
Gun.__index = Gun
Gun.__newindex = function(metaData,key,value)
if key == "Enabled" then
rawset(metaData,"Enabled",value)
print("test", value)
-- do stuff with the value variable
end
end
function Gun.new(gunInstance, owner, enabled)
local metaData = setmetatable({}, Gun)
metaData.Model = gunInstance
metaData.Owner = owner
metaData.Enabled = enabled
return metaData
end
return Gun
Try this maybe? Though you should have a separate table for the actual values so that it works a second time due to how newindex works