QuerySelector in Lua?

Hello everyone, just a quick question:

For those who know javascript, you might use .querySelector or .querySelectorAll; so is there a way to implement that in Roblox Lua. For who doesn’t what the query selector does - it gets the object’s name regardless of what their parent is in. For example: if parent1 has a child named child and parent2 has a child named child, by using querySelector, you can get te child of both parents without writing childOne = parent1.child and childTwo = parent2.child.

could you not just recursively :GetChildren() and filter by the name?

Yeah, you can traverse the DOM tree recursively with

workspace:FindFirstChild("name", true)

The second argument makes it recursive throughout the entire hierarchy just like the querySelector.

2 Likes

@MrNicNac, are you saying that workspace:FindFirstChild("name", true) will search through all of the parents and find the children with that name?

It will only return the first child found with the specified name.

It will only look through all the descendants in Workspace. If you really wanted to you could do it from game to look everywhere.

wow, I never thought of that, thanks for the replies, I’ll keep this post going to see what other people can think of.

Yes, this is actually possible. I have successfully created a way to do the following in Lua:

local S = require('path/to/QuerySelector.lua')
S('model.model .frame#someID .textbutton')

I will be open-sourcing this soon as part of a bigger module.

interesting, looking forward to this…

It is is already released, search RoQuery in #resources.