See Attributes in script

Hey I just need some quick help;

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.

You can use the :GetAttributeChangedSignal function for it work you can learn more here

I dont want to have to set each attribute I want it to detect if the script has a changed one and then show me the value and name

I have the local attributes = script:GetAttributes()

You still can use the :GetAttributeChangedSignal and if it’s changed then you can us the :GetAttribute
to see the value of it

If you want to get all of the values and names then you can use the :GetAttributes
here’s a link of it Instance:GetAttributes

You can for loop through :GetAttributes to print the name and value

How would I do that though? in the

script:GetAttributeChangedSignal(“WHAT DO I PUT HERE”):Connect(function()

end)

for _, attribute in ipairs(script:GetAttributes()) do
script:GetAttributeChangedSignal(attribute):Connect(function()
--//store callbacks in a table perhaps
end)
end
1 Like

This will only run once an attribute changes?

Yes isn’t that exactly what you wanted ?

Lets say there is 10 attributes in a script

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

You don’t have to manually do them the script @C_Sharper sent checks if one of the attributes has changed.

wont that always run? isnt there a way to only run when value changed

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

yeah but isnt it in a for loop?

Its in a for loop not a while loop you can do more research about what a for loop is and how it works in luau

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

try this

for name, attribute in ipairs(script:GetAttributes()) do
    script:GetAttributeChangedSignal(attribute):Connect(function()
        print(name, attribute)
end)
end

Does not work I also added some prints in the inside when attribute gets changed does not print

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

This will work I’ve tested it :slight_smile:

thanks just tested too, thanks for the help.