Is there a way to see if theres an exact word in the name of an instance?

So im making a script that gets every part with the name of Ventilation at the game, and i dont know how to make so it checks if it has the “Ventilation” part at the name, bc every ventilation is named like: Ventilation1, Ventilation2 and it goes like that

Can anyone help me?

try this:

for _, Ventilation in pairs(workspace:GetDescendants()) do
    if string.sub(Ventilation.Name, 1, 11) == "Ventilation" then
        -- Your code here
    end
end

This would need manual change for the numbers itself, rather just do string.find, and check it over. This would be much easier.

local descendants = workspace:GetDescendants()
for _, child in ipairs(descendants) do
    local found = string.find(child.Name, "Ventillation")
    if found then
        ... -- it's a ventillation
    end
end

Makes more sense. :+1:

A little explanation over, string.find is a function that contains the string library. It will find any matching pattern from the second argument passed in, to the first. Returning where it starts the matching till the end (index in the string as a character position). For example:

local found = string.find("Hello,", "Hello")
print(found) -- 1
1 Like

There is a way, we will say that Model is the Parent of every “Ventilation” Part then we will loop trough every Child of Model. :

for _, v in pairs(Model:GetChildren) do
end

Also, v will be the child (“Ventilation” part) then we will check his name to see if “Ventilation” is in his name with string:split() and add the Instance to a Table if true. :

local Part_That_Contain_Ventilation = {}

for _, v in pairs(Model:GetChildren) do
    if v:Isa("BasePart") then
        local current_string = ""
        local full_string = v.Name
        for _, s in pairs(full_string:split("")) do
            if current_string ~= "Ventilation" then
                current_string = current_string..""..s
            else
                table.insert(Part_That_Contain_Ventilation, v)
                break
            end
        end
    end
end

Now u can check if an Instance’s Name contain an exact word !

This is incredibly overcomplicating such a small issue, he also asked for any ventillation within the game, not from a single model (I believe as well this is messy than just keeping ventillation within a folder, which wouldn’t require this string pattern matches and so). Please, use the already provided functions/methods that are there for facility.

In addition to the stated above, I’d recommend to read some style guide to Luau and keep snake_case to Python, I hope that whoever you collab with will understand it.

Have a great day.

1 Like

I didn’t know that string.find exist, i’ll be using this now, its much easier and smaller to use.

Thx for all the help, i will mark @SiriusLatte’s answer as solution bc its the easier one

1 Like
local found = string.find("Hello,", "Hello!")
print(found) -- 1 5

This example is wrong, that would return nil as the subject string does not contain the string being searched for. Additionally, string.find returns a tuple of values which means that you need to declare additional variables for these values to be assigned to.

local start, end = string.find("Hello", "Hello")
print(start, end) --1 5
--Start and end index of the matched string.

If you need to know the specific Ventilation part then that can be achieved via string.match().

local ventilationFolder = workspace.Ventilations --Example.

for _, ventilation in ipairs(ventilationFolder:GetChildren()) do
	if ventilation.Name:match("^Ventilation") then
		local ventilationNumber = ventilation.Name:match("^Ventilation(%d+)$")
		if ventilationNumber then
			if ventilationNumber == 1 then --First ventilation.
				--Do something specific for the first ventilation.
			end
		end
	end
end

With a small test in Roblox Studio you can easily check that the code I posted it works perfectly fine, also you don’t need to assign the values that are being returned, those are just lost. Please don’t post an answer without previous research/testing, thanks.

Your answer just overcomplicates what I posted.

local found = string.find("Hello,", "Hello!")
print(found) -- 1 5

This prints nil, you’re free to try yourself.

You should probably take another look at the string library’s documentation.

https://developer.roblox.com/en-us/api-reference/lua-docs/string

Oh I made a small mistake, it wasn’t meant to be Hello!, it was meant to be just Hello which will then return the 1 5 result, my mistake when posting.

image