How to use string:find with a table?

Hello,

I tried making a small script.

for _,child in pairs(workspace.Tycoon.Buttons:GetChildren()) do
	local Search = {}
	table.insert(Search, child.Name)
end

for _,child in pairs(workspace.Tycoon.Buttons:GetChildren()) do
	
	child.config.Source:find()
end

(This isnt finished but im stuck, thats why im asking here)
How would I loop through the entire table here?
child.config.Source:find()

In example i want to search the script for every word in the table, how could I do this?

Could you please format your code? Thanks

Done. Do you know a solution? :smiley:

Are those loops in one script or are they just examples of what you think would work / things that you’ve tried?

Is this what you want to do? I don’t entirely understand your problem.

local function findTable(...)
	for _,child in pairs(workspace.Tycoon.Buttons:GetChildren()) do --loop through table
		local result = child.config.Source:find(...) --use string.find normally
		
		if result then
			--return result
			return child
		end
	end
end

print(findTable("blahblah"))

Wow, yes! That could work.
Just one more question: How could I only print the result if 2 scripts have the same result?
In example 1 script has “NameX” as a result, and Script2 has the same result. Then i want NameX to be printed.

Instead of it returning the first value it finds i think it is better if it returns a table of every valid result.

local function findTable(...)
	local results = {}
	
	for _,child in pairs(workspace.Tycoon.Buttons:GetChildren()) do --loop through table
		local result = child.config.Source:find(...) --use string.find normally

		if result then
			--return result
			table.insert(results, child)
		end
	end
	
	return results
end

print(findTable("blahblah")) --now returns a table of everything that it could find

Now if there are two or more things with the same name it will return all of them inside a table, keep in mind that it will return an empty table if it doesn’t find anything.

Yes but isnt it possible to remove all the results which can only be found 1 time?

I’m sorry but i don’t understand, the function above returns a table of every single match, if you don’t want a specific result you can simply remove it from the table.

Maybe you can tell us on a broader scope what you’re trying to achieve?

I don’t know how it’s crossed my radar. This is most likely an XY problem.

1 Like

Yes, but I only want to have a list of matches that are there twice.

In example: “X, Y, Z, F, G, X”
X is the only one that is there twice so all the other letters should be removed.

Are theses letters meant to represent child or the string in child.config.Source?

This. The result table should only consist of things which are found twice. (Its for finding bugs in my tycoon, its a bit weird lol)

Also, this just doesn’t do anything :confused:

What do you mean? on the screenshot you sent it’s printing the table, is the table not correct?

Wait, am i supposed to add child.Name here local result = child.config.Source:find(…)
Or are the three dots intentional, cuz i replaced them with child.Name so it should give me all the button names basically. But its just an empty table.

... is basically all the arguments passed

local function test(...)--no matter how many arguments i give, it will get all of them
	print(...)
end


test("a", "ghhaa", "sdana") --> prints "a  ghhaa  sdana"
test("a", "ghhaa", "sdana", "evenmore", "arguments", "yes") --> prints "a  ghhaa  sdana  evenmore  arguments  yes"

In this case i use so you can use all the arguments string.find has:

local function findTable(...)
	local results = {}
	
	for _,child in pairs(workspace.Tycoon.Buttons:GetChildren()) do
		local result = child.config.Source:find(...) --give all the arguments passed
		if result then
			table.insert(results, child)
		end
	end
	
	return results
end

But what does the script search for? It searches for “…” or not? It should search for child.Name

This also gives me any empty table, which doesnt make any sense.

... is the arguments you give, you can read the code and edit to what you desire, “blahblah” was just
a placeholder, you were supposed to change it to what you wanted to find, i do not know what you want to find.

1 Like