I want some code that can detect when an attribute is changed in a script, so bassicly if an attribute gets changed it connects to a function which then reads the attributes name and stores it and says if its set to true or false, how can I do this? I don’t want to have to manually write each attribute I want it to find the one that changed stores its name and says if its set to true or false.
for _, attribute in ipairs(script:GetAttributes()) do
script:GetAttributeChangedSignal(attribute):Connect(function()
--//store callbacks in a table perhaps
end)
end
I want some code that knows when one of those 10 changes without having to check each 10, so example; 1 attribute changes and the scripts runs through all the attributes and finds the one that has a changed value. Not manually typing each attribute
It uses :GetAttributeChangedSignal so it’ll only run once the attribute is changed if it uses a while loop it’ll always run that’s why you should consider using :GetAttributeChangedSignal as its much better for the performance and runs once its changed
how would I print the attribute name and value? I tried
print(attribute.Name)
print(attribute)
for _, attribute in ipairs(script:GetAttributes()) do
script:GetAttributeChangedSignal(attribute):Connect(function()
print("test")
if attribute then
print("On")
else
print("Off")
end
end)
end
nothing is printing when attirbute is being changed
for name, attribute in ipairs(script:GetAttributes()) do
script:GetAttributeChangedSignal(attribute):Connect(function()
print(name, attribute)
end)
end
local MainInstance = script -- The object instance you want to detect attribute changes
for name, value in pairs(MainInstance:GetAttributes()) do
MainInstance:GetAttributeChangedSignal(name):Connect(function()
print(name,MainInstance:GetAttribute(name))
end)
end