LuaU Type Checking Paths

This may seem like a pretty basic question, but I’m drawing a blank. How would one go about type checking paths in LuaU? Or is this not an intended function of type checking?

Ex: I have a folder in workspace that I want to reference in a variable, say:
local folder: Folder = workspace.MyFolder

Now, I also want to reference a brick inside that folder with a variable, say:
local brick: Part = folder.MyPart

This does not align with strict type checking, as MyPart is not a property of a folder type (and thus I get an error along those lines). Where am I going wrong here?

1 Like

You could instead use FindFirstChild() for cases like these, if I am understanding your question correctly.

local Part = folder:FindFirstChild(“MyPart”)

I was considering using that, but I didn’t know if there were any performance implications of the resulting code:

local brick: Part = (folder:FindFirstChild(“MyPart”) :: Part)

is brick: Part a notation thing? I haven’t seen that before actually.

I would assume that FindFirstChild() does indeed use slightly more resource than just indexing. You probably know this already but the difference of using the two is so miniscule that you should not worry about it until the time actually comes, because it is unlikely that using this function instead of indexing would cause any impact to performance.

FindFirstChild() is also safer to use in general. Because it doesn’t error when the child doesn’t exist, but rather returns nil. Therefore you can use it to check if a child exists and use that for a condition. However if you were to index and assume that something exists, that could cause errors later on. In this case it doesn’t really matter though as the folder should exist anyway.

It is a notation, specifically a type checked Luau variable. You can read more about that here: Luau - Luau

1 Like

well I have being programming in roblox for a 2 years now, but never seen anyone defining his variables like this… anyways this is how it should look like.

local myPart = folder.MyPart

in addition of :FindFirstChild() function, we only use it if we arent sure if an object currently exists in the game (in case u destroy a part midgame)

Hey there! Your definition is perfectly fine, this question specifically pertains to Luau type checking which isn’t necessarily used by everyone around these parts. I’d really recommend you look into it a bit at the link I provided earlier in the thread, as it’s an interesting look into a few standards that are standard industry practices.

1 Like

You should only type check variables that aren’t initialized.
That goes for function parameters, values that are most of the time nil, etc.
The only exception are tables and results of special methods such as Workspace:Raycast(), you should always use custom types to note tables.
Both of the examples you gave is valid syntax, there is just not too much point in noting those as you already know what those are.

That’s just how it is for now; the type checker is still being worked on and doesn’t acknowledge children of instances.
(Also, it’s “Luau”)

2 Likes

Thanks for some clarification, I really appreciate it.

(I realized that after I posted this, my bad lol)

1 Like