Gsub not replacing regular character if it went through a gsub function first?

Im trying to replace the # with a $ in the text from the image but for whatever reason it never replaces it when it goes through a different function with a gsub inside of it first.

I have to escape the special characters in order for other characters to get replaced properly but it looks like thats impossible because it would break regular characters if i tried escaping others.

Why does this happen and is there a way to fix this?

image

This works:

local function esc(x)
	return x
end
print(string.gsub("for i,Value in # do\n!\nend",esc("#"), esc("$")))

and this does not:

local function esc(x)
	return string.gsub(x,' ', ' ')		
end
print(string.gsub("for i,Value in # do\n!\nend",esc("#"), esc("$")))

It seems to be because when you do “return string.gsub” you are actually returning two values (the result of the replacement, but also the index of the replacement).