I’m trying to use text filtering on the text the player inputs, but I get the error “string expected got instance”. Is anyone able to help?
game.ReplicatedStorage.ChangeStageText.OnServerEvent:Connect(function(player, text)
local function getFilteredMessage(textObject, toPlayerId)
local TextService = game:GetService("TextService")
local filteredTextResult = TextService:FilterStringAsync(textObject, toPlayerId)
return filteredTextResult
end
script.Parent.SurfaceGui.TextLabel.Text = getFilteredMessage(text, player.UserId)
end)
The TextService:FilterStringAsync() sends back and instance as the return.
Use this instead it should work.
game.ReplicatedStorage.ChangeStageText.OnServerEvent:Connect(function(player, text)
local function getFilteredMessage(textObject, toPlayerId)
local TextService = game:GetService("TextService")
local filteredTextResult = TextService:FilterStringAsync(textObject, toPlayerId)
local filteredString = filteredTextResult:GetNonChatStringForUserAsync(toPlayerId)
return filteredString
end
script.Parent.SurfaceGui.TextLabel.Text = tostring(getFilteredMessage(text, player.UserId))
end)
Wrapping the returned value inside a tostring() call is unnecessary as the function only ever returns a single string type value.
@Towren
Is this filtering performed for chat messages between users or something like naming a pet? If the latter then keep using GetNonChatStringForUserAsync() as that’s what it is purposed for, if the former, then you should use GetChatForUserAsync() as that is supposed to be used to filter chat messages shared between users.