String expected, got Object after sending filtered message to client

I created a topic like this by accident, please no bully

I’ve got two scripts, one handling messages and another handling the filter. Everything works fine until the serverside filter tries to send the filtered message to the client.

This is the script I use to filter messages

local textservice = game:GetService("TextService")

game.ReplicatedStorage.RadioOut.OnServerEvent:Connect(function(player, message)
    local message = textservice:FilterStringAsync(message, player.UserId, Enum.TextFilterContext.PublicChat)
    local filteredMessage = message:GetChatForUserAsync(player.UserId)
    game.ReplicatedStorage.RadioIn:FireAllClients(player, filteredMessage)
end)

This is the localscript section throwing the error

local f = localplayer.PlayerGui:WaitForChild("rado goowey").msgs
reventin.OnClientEvent:Connect(function(filteredMessage)
    -- stuff
    f.TextLabel1.Text = filteredMessage
end)

I know part of the script(s) work because I have another section that places the name of the player next to the message. Thanks in advance, further questions are welcome.

The FilterStringAsync doesn’t return a string, but a TextFilterResult object. You should call appropriate method (GetChatForUserAsync, GetNonChatStringForBroadcastAsync, …) on it to get actual text.

1 Like

Could I put it in the same function like in the script, or it it better to separate it?

Doesn’t matter that much but I’d keep them together.

I’ve tried doing this in conjunction with the wiki, however it still returns the same error.

game.ReplicatedStorage.RadioOut.OnServerEvent:Connect(function(player, message)
	local result
	local success, errorMessage = pcall(function()
		result = textservice:FilterStringAsync(message, player.UserId, Enum.TextFilterContext.PublicChat)
	end)
	local filtered
	local success, errorMessage = pcall(function()
		filtered = result:GetNonChatStringForBroadcastAsync()
	end)
	game.ReplicatedStorage.RadioIn:FireAllClients(player, filtered)
end)

You’re setting the text to the player. If you’re using FireAllClients, you don’t need to specify a player, since it’s going to all clients. (Firing the player will just make the first argument in OnClientEvent be that player.)

3 Likes