EDIT 2: Turns out filtering only works in normal roblox, not in studio. thanks to https://devforum.roblox.com/t/how-to-use-the-filterstringasyncs-yield-functions/81735
My working script is this:
textService = game:GetService("TextService") game.ReplicatedStorage.NameChange.OnServerEvent:Connect(function(Player, Text) local FilteredText = "" local suc,err = pcall(function ()--filters name input FilteredText = textService:FilterStringAsync(Text, Player.UserId) end) print(Text) print(FilteredText) FilteredText = FilteredText:GetNonChatStringForBroadcastAsync() print(FilteredText) if not suc then--if filtering failed, name player the error Player.Character.Head.BillboardGui.TextLabel.Text = "#" print('filtered') end if suc then Player.Character.Head.BillboardGui.TextLabel.Text = FilteredText print("Success") end end)
EDIT: So I ran this in the console (warning it contains a swear word)
print(game.TextService:FilterStringAsync('shit', game.Players.UNBANhappyniceguy5.UserId):GetNonChatStringForBroadcastAsync())
and it just printed the swear word uncensored... am I doing something wrong?
ORIGINAL: So I have a custom role playing name script, but whatever I write gets filtered. Here's the script
textService = game:GetService("TextService") game.ReplicatedStorage.NameChange.OnServerEvent:Connect(function(Player, Text) local FilteredText = "" local suc,err = pcall(function ()--filters name input --name = chatService:FilterStringForBroadcast(Text,Player) print("attempt") FilteredText = textService:FilterStringAsync(Text, Player) print(FilteredText) end) print(suc) if not suc then--if filtering failed, name player the error Player.Character.Head.BillboardGui.TextLabel.Text = "#" print('filtered') end end)
It seems to not print the filtered text at line 10 so I guess the script is just skipping line 9 which is FilteredText = textService:FilterStringAsync(Text, Player)?
This is my first time doing filtering so I have no idea what's wrong.
Found an answer here
But what really ticked me off is your use of pcall
.
You don't need to pass a function literal, you can just pass a reference to the FilterStringAsync
method directly.
Maybe this will miraculously work and it's just something you did wrong and you didn't see it
local textService = game:GetService("TextService") -- pls use local variable local successful, result = pcall(textService.FilterStringAsync, textService, Text, Player) if successful then local result_success, filtered = pcall(result.GetNonChatStringForBroadcastAsync, result) Player.Character.Head.BillboardGui.TextLabel.Text = successful and filtered or "#" end
If pcall
returns true
(it always returns a boolean first), then the return values after are the return values of the function passed. pcall
also takes multiple arguments, not just a function. The arguments after the function are the arguments to the function passed. a:b(...)
is syntactic sugar for a.b(a, ...)
so textService
is being passed as argument there so Lua knows what to call the method on
Turns out filtering only works in normal roblox, not in studio. thanks to https://devforum.roblox.com/t/how-to-use-the-filterstringasyncs-yield-functions/81735