Problem calling string.match multiple times in quick succession
LiamHoffen Resident
Running on server: Luau 2026-01-06.20757451310
In successive calls of string.match, I often get a fatal error if I don't wrap string.match in a pcall:
runtime error
lua_script:3: String pattern too complex, timed out
[C] function match
The problem is that the only thing repeatable is that I may or may not get errors when running the code.
local function getStringMatch(s)
local status, k, v
repeat
status, k, v = pcall(string.match, s, "(%g+)%s*=%s*(%g+)")
if not status then print(k) end
until status
end
local keys = {}
for i = 1, 10 do
table.insert(keys, tostring(ll.GenerateKey()))
end
for k, v in keys do
print(k)
getStringMatch(v)
end
This is basically a useless example, except to trigger the issue. gmatch should return the captures, the original string if no captures, or nil.
This example will never find a match, but I don't believe string complexity is the issue, and I don't believe its timing out because it happens very fast.
But, if a failure is triggered, I can recall the very same match again and get success, without providing any delays in the code.
Log In
SuzannaLinn Resident
Pattern matching is slow. Depending on when it starts within the script's assigned time slice, it might not have enough time to finish, causing an error.
More details in a previous canny: https://feedback.secondlife.com/slua-alpha/p/stringmatch-can-trigger-unable-to-perform-mandatory-yield-error
The workaround is to add a sleep before the operation. This waits until the next frame, ensuring the full time slice is available to execute the function:
local function getStringMatch(s)
local status, k, v
repeat
ll.Sleep(0.00001)
status, k, v = pcall(string.match, s, "(%g+)%s*=%s*(%g+)")
if not status then print(k) end
until status
end