String.gsub not working & Filter?

So below there is an GUI which was made to fire an event to the server to change the players custom tag above there head but I ran into this issue where the string wont remove the brackets and filter?

Error: ServerScriptService.UpdateTag:13: malformed pattern (missing ‘]’)
Code:

local mps = game:GetService("MarketplaceService")
local ts = game:GetService("TextService")
local rem = rep:WaitForChild("UpdateTag")
local filteredTextResult = ""

rem.OnServerEvent:Connect(function(player, tagText, tagFont, tagColorR, tagColorG, tagColorB, tagStroke)
	local owns = mps:UserOwnsGamePassAsync(player.userId, 6659891)
	if owns then
		local tag = game.Workspace:WaitForChild(player.Name).Head.Nametag.Frame.VIP
		local brackets = string.find(tagText, "]", 1)
		local unb = string.gsub(tagText, "]", "")
		local unb2 = string.gsub(unb, "[", "")
		if brackets == true then
			filteredTextResult = ts:FilterStringAsync(unb2, player.userId)
		else
			filteredTextResult = ts:FilterStringAsync(tagText, player.userId)
		end
		
		if brackets == true then
			tag.Text = "["..filteredTextResult.."]"
		else
			tag.Text = filteredTextResult
		end
		tag.TextColor3 = Color3.fromRGB(tagColorR, tagColorG, tagColorB)
		tag.Font = tagFont
		tag.TextStrokeTransparency = tagStroke
	end
end)

The character [ is used in string pattern syntax. You need to escape it with \ beforehand, like so:

local unb2 = string.gsub(unb, "\[", "")
1 Like

Sorry for the disturbance but I’m now getting the error: " ServerScriptService.UpdateTag:16: attempt to concatenate global ‘filteredTextResult’ (a userdata value)"

		local success, errorMessage = pcall(function()
 			filteredTextResult = ts:FilterStringAsync(tostring(tagText), player.UserId)
		end)
		if success then
			if tagBrackets == true then
				tag.Text = "["..filteredTextResult.."]"
			else
			tag.Text = filteredTextResult
		end

filteredTextResult is not a string, try to use tostring().

FilterStringAsync() returns a TextFilterResult instance named “Instance”. There are a number of functions here which can be used to return an appropriately filtered string depending on your needs.

OP has moved secondary question here.

3 Likes