Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Very Strange issue with Filtering Strings?

I am trying to make a chat system...

Server Code:

game.ReplicatedStorage.Remote.Event.Chat.OnServerEvent:Connect(function(plr,txt)
		local TextService = game:GetService("TextService")
		local filteredText = "[ERROR WITH FILTERING]"
		local success, errorMessage = pcall(function()
			filteredText = TextService :FilterStringAsync(txt,plr.userId)
		end)
		if success then
		end
	game.ReplicatedStorage.Remote.Event.Chat:FireAllClients(plr,filteredText)
end)

Client Code:

game.ReplicatedStorage.Remote.Event.Chat.OnClientEvent:Connect(function(playr,txt)
	print(txt) -- returns nil?
	local Text 
	pcall(function()
		Text = txt:GetChatForUserAsync(Player.userId) 	
	end)
	renderMsg(playr,(Text or txt) or "[ERROR WITH FILTERING]")
end)

In the server code, [txt] is an Instance, which is what is supposed to happen, but when I shoot the instance over to the client, it says that [txt] is nil

1 answer

First of all, you should call GetNonChatStringForBroadcastAsync() on filteredText before you send them to clients. The instance probably is not replicated, so it is nil on the client. Also, you should NOT send any text if the filter does not succeed, nor trying to call it again, as ROBLOX has its own retry method. With this change, your server code will look like

game.ReplicatedStorage.Remote.Event.Chat.OnServerEvent:Connect(function(plr,txt)
        local TextService = game:GetService("TextService")
        local filteredText = txt --Don't you want to filter the actual text the user sends?
        local success, errorMessage = pcall(function()
            filteredText = TextService :FilterStringAsync(txt,plr.userId):GetNonChatStringForBroadcastAsync()
        end)
        if success then
		game.ReplicatedStorage.Remote.Event.Chat:FireAllClients(plr,filteredText)
	else
		warn('Could not filter '..plr..'\'s message!')
        end
end)

Here is the link for this.

Hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.