Filtering message error ''Unable to cast null to token''

Hey I wrote this code to filter what the person types before sending an event, and It gives a ‘‘Unable to cast null to token’’ error does anyone know how can I fix it, thanks!

The code:

local ReplicatedStorage = game:GetService("ReplicatedStorage"):WaitForChild("HereEventFolder")
local event = ReplicatedStorage.MainEvent
local button = script.Parent
local textBox = script.Parent.Parent.TextBox
local TextService = game:GetService('TextService')
local player = game.Players.LocalPlayer.UserId

button.MouseButton1Click:Connect(function()
	local msg = textBox.Text
	local filteredmessage = TextService:FilterStringAsync(msg, player, Enum.FilterResult)
	event:FireServer(filteredmessage)
end)

Don’t you think it would be better if the server took care of string filtering?
Imagine if an exploit could send an unfiltered message to the server and the server had to display it?

1 Like

take a closer look at what arguments you need to enter into this method
Screenshot_20250225-233624~2

hmmm sure it would be better but I have multiple scripts sending events to that server and the other ones don’t need to get filtered so doing it in a local script is better and it is a small project so I don’t think that it will be a big problem

I changed it to this and it still doesn’t work

local filteredmessage = TextService:FilterStringAsync(msg, player, Enum.TextFilterContext)

You need to choose Enum.TextFilterContext.PublicChat or Enum.TextFilterContext.PrivateChat

I did it and now it says this I guess I will rewrite in a server script for it to be more secure
image

Yes, because chat is a global thing

I tried to say this without saying it directly.
The server should always take care of the secure part of the game, especially related to chat.
If you look at examples of this function in the docs for this service, you will see that all the examples are used in a script.

1 Like

It says this do you know why?
image

Try this

local filteredmessage = TextService:FilterStringAsync(msg, player.UserId) -- You need the UserId, not the player.
filteredmessage:GetNonChatStringForBroadcastAsync()
-- Now you have the text.

If you are working in a public chat for multiple users (containing different languages ​​and countries), I recommend using the above function, as it serves exactly that purpose.

it is still giving the same error I think it is looking for a number but it is a normal text???

My current code is this:

local GelenEvent = game.ReplicatedStorage.HereEventFolder:FindFirstChild('FiltrelemeEvent')
local GidenEvent = game.ReplicatedStorage.HereEventFolder:FindFirstChild('FiltrelenmisMesajEvent')
local TextService = game:GetService('TextService')

local function filtreleme(msg, Player)
	local filteredmessage = TextService:FilterStringAsync(msg, Player) -- You need the UserId, not the player.
	filteredmessage:GetNonChatStringForBroadcastAsync()
	GidenEvent:Fire(filteredmessage)
end

GelenEvent.OnServerEvent:Connect(filtreleme)

local filteredmessage = TextService:FilterStringAsync(msg, Player.UserId)

–like @Candy_Project said