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)
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
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)
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.
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
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.
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.