Hello DevForum!
Lately I’ve been working on a number spinner module, and I’m running into an issue. Basically, I’ve been trying to find a way to detect value changes in a table, and I did find a workaround.
function Spinner.new(properties)
local self = typeof(properties) == "table" and properties or {
Frame = nil,
Value = "0",
Duration = 0.25,
Alignment = "Right",
}
self.Value = self.Value and tostring(self.Value) or "0"
setmetatable(self, Spinner)
self:CreateDigits()
return setmetatable({}, { -- my weird "proxy" method which works apparently
__metatable = self,
__index = function(_, index) return self[index] end,
__newindex = function(e, index, value)
if index == "Value" then self:Spin(value)
else self[index] = value
end
end,
})
end
It works fancy and all but here’s the issue: if I try to print the object it returns (the “proxy” metatable), it prints an empty table
local spinner = spinnerModule.new{
Frame = frame,
--Value = "0",
Duration = 0.5,
Alignment = align,
Padding = 0,
LabelProperties = {
Font = Enum.Font.Gotham,
TextColor3 = Color3.fromRGB(255, 255, 255)
}
}
counter:GetPropertyChangedSignal("Value"):Connect(function() -- Counter is an IntValue in ReplicatedStorage
spinner.Value = counter.Value
print(spinner) -- prints {}
print(spinner.Value) -- prints 5 because of the __index as expected
end)
So is there any way if i do print(spinner)
it prints the self
table? I’ve been researching but couldn’t find any help. I’m pretty sure that even the “proxy” method I’m using to detect a value change is inefficient (I came up with that myself after over two whole hours of experimenting ), so if you’ve got any workarounds, please do suggest.
Oh yeah and I’m bad at explaining so if I’m not clear please tell.
Thank you in advance,
Vex
P.S: I’m trying to avoid BindableEvents to make the event on purpose.