Invalid use of '%' in replacement string string.gsub

Hello,
I am trying to replace magic characters with magic characters.
Or more specifically, parentheses with curly brackets.

But for some reason, I keep getting this error: invalid use of ‘%’ in replacement string.

I can’t seem to find a solution to this.

local x = "(hi)"
local y = "{hi}"
x = x:gsub("%(", "%{")
y = y:gsub("%{", "%(")
x = x:gsub("%)", "%}")
y = y:gsub("%}", "%)")

Could someone help me fix this problem?
Thanks.

1 Like

I figured it out. Just put % twice.

local x = "(hi)"
local y = "{hi}"
x = x:gsub("%%(", "%%{")
y = y:gsub("%%{", "%%(")
x = x:gsub("%%)", "%%}")
y = y:gsub("%%}", "%%)")
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.