Using events FilterStringAsync() does not return String

need help filtering text, using events to pass text to server and then filtered result back to client. I dont know why it’s always passing back nil, the player or Instance. Note that the “hi” is what was actually in the text box.

client:

InfoBox.FocusLost:Connect(function()
    task.wait(0.1)
    if InfoBox.Text ~= nil then
        print(InfoBox.Text)
        SendForFilter_Info:FireServer(player, InfoBox.Text)
    end
end)

FilteredText_Info.OnClientEvent:Connect(function(player, result)
    print(tostring(result), result)
    InfoBox.Text = result
    BasicInfo.Value = result
end)

server:

SendForFilter_Info.OnServerEvent:Connect(function(player, text)
    print(text)
    local FilteredText

    local success, err = pcall(function()
        FilteredText = TextService:FilterStringAsync(text, player.UserId)
    end)

    if not success then
        warn(`[SpriteService] Error filtering text: {text}. UserId: {player.UserId}`)
        FilteredText_Info:FireClient(player, "Error filtering text")
    else
        local FilteredString = FilteredText:GetNonChatStringForBroadcastAsync()
        print(FilteredString)
        FilteredText_Info:FireClient(player, FilteredString)
    end
end)

image

1 Like

The reason this code does not work is because you are getting “Player” as the string on the client, instead of “result”. This is happening because OnClientEvent does not automatically take the Player as a parameter on the client. To fix it, either remove the “Player” parameter from the client, or pass the player twice from the server (Ex: FilteredText_Info:FireClient(player, player, FilteredString) ).

1 Like

Oh okay sorry I thought that might have been the issue. It’s been a while since I’ve scripted a lot. I will try this out and see what happens.

1 Like

image
Ok I did that, but it’s still returning nil when I try to set the text on the client.

FilteredText_Info.OnClientEvent:Connect(function(player, FilteredString)
	print(tostring(FilteredString), FilteredString) --> nil nil
	InfoBox.Text = FilteredString --> Unable to assign property Text. string expected, got nil
	BasicInfo.Value = FilteredString
end)

But on the server it is printing the correct text.

		local FilteredString = FilteredText:GetNonChatStringForBroadcastAsync()
		print(FilteredString) --> whatever the filtered result is
		FilteredText_Info:FireClient(player, FilteredString)
1 Like

Your main issue is when you receive the OnClientEvent. There should only be one parameter in the OnClientEvent, which is FilteredString. To fix this, simply remove the first parameter which is “player” (Do this on the local script in which the event gets received!)

2 Likes

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