I’m making a system that tweens the transparency of every instance in a frame which has a tweenable transparency property (i.e BackgroundTransparency, ImageTransparency, TextTransparency, etc).
I don’t wanna do it with a big if statement, so I made an array of each tweenable property and I iterate through it and attempt to access each property in my target Frame instance. If it’s there then I add it to a goal table and use TweenService to tween it. The only issue is that accessing a nonexistent property always throws an error. Do I need to pcall this or is there a built in method I can use to get a property from a string?
The only way of checking for Properties is by checking for an objects class, which is way more efficient and faster than a pcall()
A pcall() should only be used for errors you cannot control, such as DataStores, and Http Requests, otherwise you are just using a pcall() for no reason at all, so you dont need it, if statements are you best friend here, there shouldn’t be a real reason not to use them when they are pretty fast at their job. And very useful in there execution.
You can access properties by using brackets instead of a dot, like:
Object.Position
Object["Position"]
In your case, instead of checking if the property is on an object, you could use :IsA() to get the objects type, every MeshPart will have a MeshId, every ImageLabel will have ImageTransparency, etc.
I feel like hardcoding types would get pretty messy seeing as how there are many different GUI objects with many different properties which will affect the transparency in different ways.
type o = (VideoFrame | Frame | UIGradient | UIStroke | ViewportFrame | ImageButton | ImageLabel | ScrollingFrame | TextBox | TextButton | TextLabel | CanvasGroup)
local function getTransparencies(o: o): {string}
local c = o.ClassName
local t = "Transparency"
local bt, it = "Background"..t, "Image"..t
if c == "VideoFrame" or c == "Frame" then
return {bt}
elseif c == "UIGradient" or c == "UIStroke" then
return {t}
elseif c == "ViewportFrame" or c == "ImageButton" or c == "ImageLabel" then
return {bt, it}
elseif c == "ScrollingFrame" then
return {bt, "ScrollBar"..t}
elseif c == "TextBox" or c == "TextButton" or c == "TextLabel" then
return {bt, "TextStroke"..t, "Text"..t}
elseif c == "CanvasGroup" then
return {"Group"..t, bt}
else
return {}
end
end
It returns an array of all the edible transparencies. If empty then the class isn’t a valid option.
PS: This is related to UIs. Decals, textures, and parts aren’t included.