Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Getting children with a certain starting name?

So I have a gui full of buttons and I was wondering how do I get the strings that have a certain starting name, for example if the gui contains:

Model1 Model2 Part1 Part2

How would I getChildren() of a certain starting name, for example getting all of the items that start with "Model".

Note: Ignore the classnames, that's not what I am trying to get.

2 answers

✓ Accepted answer

Hello, iFlusters. There are 2 things that you need to use in order to accomplish this task, the first is called a 'for' loop, you can learn more about those on the wiki. The second thing you need is a function called 'string.find', string.find looks to see if a string is within another string, and then returns the position of it. Now that we have the stuff we need, we can write the code.

local Object = workspace --Object that you are going to search for
local String = 'Model' --The string you want to see if the object contains
local Objects = {} --Objects that don't have the String within their name
for _,Obj in pairs(Object:GetChildren()) do
	if not(string.find(Obj.Name,String)) then
		table.insert(Objects,Obj)
	end
end

Well we would use iterations to check the object for any model We would use an if then statement to check if the model fits the criteria

for i,v in pairs(game.Workspace:GetChildren()) do -- iterates
	if v.Name:sub(1,5) == "Model" then -- sub splits text so only if the first 5 letters are Model
		print(v.Name.."is a model")
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.