String:Find() Error unfinished capture

Hello DevForm member! I currently have a function here that is supposed to return me in a table whenever it finds the words to search for. After several checks I had solved my problem, I sent an object and find and not a string. But now I have a new problem that I don’t understand. I can’t find anything on the devForm or on the internet
So the error is user_AssetsWatcher.rbxmx.AssetsWatcher.Main:97: unfinished capture (this is a plugin)
and this is the function

local function findAll(obj, sub)
	local found = 0
	local positions = {}
	while(found)do
		found = found + 1
		found = obj:find(sub, found)
		table.insert(positions, found)
	end
	return positions
end
local search = findAll(object.Source, "Hi!") -- object is a script

The line that cose the error is found = obj:find(sub, found)

1 Like

Are you doing this from a Plugin Script? or a normal script?

1 Like

Plugin Script
caracterrrrrrrrrr

1 Like

Try putting a wait timer

while(found)do
	found = found + 1
	found = obj:find(sub, found)
	table.insert(positions, found)
    wait() -- to prevent it from freezing
end
1 Like

No :confused: This is don’t work same error
image

1 Like

Ok, this is probably because the third argument for string.find specifies where to start the search and string.find either returns nil or 2 numbers (start, end)

Try

while(found)do
	found = found + 1

    local start, _end = obj:find(sub, found)
	found = _end

	table.insert(positions, {start, _end})
    wait() -- to prevent it from freezing
end
1 Like

No, nothing changed :confused: this is the same error

1 Like

The last things I can assume is that this is a roblox error since it’s erroring from roblox plugins or that the script’s (obj) source has an issue

1 Like

:confused: The script source is just a normal script and if i do something like this object.Source:find("require(",1,true) this is working (this is not what i want)

1 Like

wait, i think I just figured it out “i think

try doing

local search = findAll(object.Source, "Hi%!")
1 Like

In fact I changed the string to search for the post the real string to search for is “require(” so i don’t think is will work

1 Like

Have you tried escaping the string? Instead of doing:

found = obj:find(sub, found)

Just do:

found = obj:find("\" .. sub, found)
1 Like

The cause of your error is an unfinished string pattern capture. I don’t think your actually trying to use a string pattern but your program thinks so. you can escape it like said above.

1 Like

string.find uses ‘regular expressions’ which is a system built for string manipulation. You need to ‘escape’ any special characters so that it doesn’t think you’re trying to program things.

Does that actually work? Can you just make the whole string a literal like that? Also, ironically, you forgot to escape your backslash and will encounter a similar problem.
found = obj:find("\\" .. sub, found)

This should fix your code, but it won’t be efficient and it is better to fix it at the source, wherever you call findAll.

local function esc(x)
   return (x:gsub('%%', '%%%%')
            :gsub('^%^', '%%^')
            :gsub('%$$', '%%$')
            :gsub('%(', '%%(')
            :gsub('%)', '%%)')
            :gsub('%.', '%%.')
            :gsub('%[', '%%[')
            :gsub('%]', '%%]')
            :gsub('%*', '%%*')
            :gsub('%+', '%%+')
            :gsub('%-', '%%-')
            :gsub('%?', '%%?'))
end

local function findAll(obj, sub)
    sub = esc(sub)
	local found = 0
	local positions = {}
	while(found)do
		found = found + 1
		found = obj:find(sub, found)
		table.insert(positions, found)
	end
	return positions
end
1 Like

If you escape the backslash then you would just get \yourstringhere. When I tested it in studio using just one \ the string was escaped properly, getting rid of the error.

Edit: Nevermind, this method seems to not work. The example you provided seems a lot better.

1 Like

Thx is working ! Thanks to everyone else who tried to help me too.
@JarodOfOrbiter do the name of the function esc have a signification or not ?

When it’s local function whatever(), the name doesn’t matter. It could be literally anything.

2 Likes

Srr, i will mean do “ESC” is a abbreviation of somethings ?

It’s not significant, so long as if you rename it you also rename the places it is called so you don’t get an error.
I just yanked that off the internet because I’m lazy, I didn’t write it myself.

I searched “Escape whole string Lua” and found this, where I carelessly plagiarized like the programmer I am.

3 Likes

Oh, yeah, it’s probably just short for escape.

3 Likes