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?
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 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.
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.
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)