Is there a way to "safely" access a property?

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?

4 Likes

wrap it in a pcall and hope for the best

3 Likes

I ended up doing this but it feels gross. There should be safe getters and setters for properties!!!

4 Likes

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.

1 Like

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.

2 Likes

Object.Position
Object[“Position”]`

This would still throw an error

Yes, which is why I recommended using IsA() and checking the objects type, instead of trying to check if an object has properties.

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.

Have fun:

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.

2 Likes

Lol nice, I ended up just using pcall. Imagine how convenient instance:GetProperty would be though…

1 Like

That exists? Isn’t that just:

if Inst[Property] then
	--...
end```

Yes, but it can cause an error if the property does not exist.

The error in question: {example} is not a valid member of {Instance}

1 Like

Why don’t you just use a CanvasGroup | Documentation - Roblox Creator Hub? You could just tween one instance instead of writing sloppy code.

1 Like

I didn’t know these existed lol, thanks

CanvasGroups are very prone to performance issues because they create a new texture every frame. I would avoid using those.

1 Like

I’ve never had any performance issues. Mind sharing your source? Much appreciated.

(scroll down to Important notes:)

Edit: The embeds aren’t showing what I linked to, here they are:

image

image

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