How to use string:find with a table?


Well i cant use child.Name there. :confused:

You want to search for the source’s ancestor’s name?

local function findTable()
	local results = {}

	for _,child in pairs(workspace.Tycoon.Buttons:GetChildren()) do
		local result = child.config.Source:find(child.Name) --search child.Name
		if result then
			table.insert(results, child)
		end
	end

	return results
end

print(findTable())

Its still giving me a completely empty table. Idk whats wrong with it :confused:

Why is it inserting the child here table.insert(results, child)
It should insert “result” or not?

Alright, this is not going anywhere, please explain what you want to do entirely, like i said on my earlier post.

What is “Source”? And what exactly are you trying to do here? Could you just instead use a module script.

1 Like

Ok so, my tycoon system has two folders

  • Buttons
  • Purchases

In each button is a config script, which has this line "Dependencies: {X, X2, X3} There can be 0-infinite X.
A button name (X is the button name for now) can only be used as a dependency in one config script. But if I made a mistake a button name might be in two config scripts, which will break the game sometims. So I gotta find out if any button name is in 2 or more config scripts.

Not sure if a module script would work but i think command bar is quicker and simpler? Source is just the inside of the script or not

Are you talking about the script.Source property? If so, I don’t think that this would work, as script.Source is formatted in bytecode I believe.

I see, so this config is just a script file, and you are accessing the inner text. Module script should still work:

local function findTable(...): ...any
	local args = { ... }
	local results = {}
	
	for _, child in ipairs(workspace.Tycoon.Buttons:GetChildren()) do
		-- assuming this is now a module script
		local config = require(child.config)
		
		for _, value in ipairs(args) do
			local indexOf = table.find(config, value)
			
			if indexOf then
				table.insert(results, config[indexOf])
			end
		end
	end
	
	return unpack(results)
end

What do I do with this? Sorry. Where do I add what im searching for? And will it only display the ones where it found 2 results?

You can use a loop to iterate through the table to search for every word. For example, you can use a for loop to loop through each element in the table and use the string.find() method to check if the word is in the string.

For example:

for i,word in ipairs(Search) do
    if string.find(child.config.Source, word) then
        --do something
    end
end