How would you dynamically change the properties of an Instance from a table containing multiple properties?

Not sure if this is the best way to word it but

What do you want to achieve? I want to “dynamically” change the properties of an Instance (Frame) from a table containing multiple property names.

Basically, I have this table which are the changes applied to a Frame by the user:

local propertiesToApply = {
   BackgroundTransparency = 0.5,
   BackgroundColor3 = Color3.new(0,0,0)
}

Once the user clicks a button I want to create a frame with those properties.
My approach here is:

local frame = Instance.new("Frame")
frame.Name="ExportFrame"
for key, value in pairs(propertiesToApply) do
	print("Applying ".. key.. " of ".. value)
	-- I want to apply the "key" (BackgroundTransparency) to the frame with the "value" being (0.5)
    -- can I do frame[key] = value ?
end
frame.Parent = ScreenGui

will frame[key] = value work here or is there a built in function to change properties of an Instance that I don’t know of?

Thank you for helping :sparkling_heart:

Yes. The linter will warn you it is not possible though. If you want to silence it, you can use

;(inst :: any)[key] = value

Also, you may need to wrap it with pcall in case of read-only and non-existent members.

If you can, you can use ReflectionMetadata to find all the properties for a class of an instance.

2 Likes

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