Still confused about Text formatting a server-sided text without UserId

Hi people.

I have an API that would give me some text about the traffic of the Parisian metro. But since it’s ROBLOX, I’d rather filter the texts to avoid surprises. What I want to do is to display the message on a screen (SurfaceGui > TextLabel).

But after some research, I still couldn’t figure out how to filter a server-given text without having to provide a UserId.

Although I am aware of functions like TextService:FilterStringAsync() or ChatService:FilterStringForBroadcast(), both of them require an UserId.

Say I have a simple text (from which I already deleted some links),

local Text = "Traffic is disturbed on line 4 because of a passenger incident."

how do I filter that and broadcast it on a screen that all players can see?

Thank you for helping.
Louis.

On a screen, you mean GUI, or SurfaceUI?

A SurfaceGui on a BasePart in the Workspace.

I wrote a quick function:

local TextService = game:GetService("TextService")

local function filterText(text)
    local filteredText = "Message could not be filtered."
    local success, errorMessage = pcall(function()
        local filterResult = TextService:FilterStringForBroadcast(text)
        filteredText = filterResult:GetNonChatStringForBroadcastAsync()
    end)
    if not success then
        warn("Filtering failed: " .. errorMessage)
    end
    return filteredText
end

It does warn me that

Filtering failed: FilterStringForBroadcast is not a valid member of TextService "TextService"

as I have already found in the documentation, FilterStringForBroadcast is a function of ChatService and requires a UserId.

I would just use the first player In the player list, since the player isnt going to send the message anyway and it won’t be visible in chat:

local TextService = game:GetService("TextService")
local Players = game:GetService("Players")

local function filterText(text)
	local filteredText = "Message could not be filtered."
	local player = Players:GetPlayers()[1]
	
		local filterResult = TextService:FilterStringAsync(text, player.UserId)
		filteredText = filterResult:GetNonChatStringForBroadcastAsync()
	
	return filteredText
end

Yeah I ended up accepting that there must be a player / UserId to filter my texts.

local ChatService = game:GetService("Chat")

local function filterText(text)
	local filteredText = "Message could not be filtered."
	while #game.Players:GetPlayers() < 1 do
		wait()
	end
	local success, errorMessage = pcall(function()
		filteredText = ChatService:FilterStringAsync(text, game:GetService("Players"):GetPlayers()[1],game:GetService("Players"):GetPlayers()[1])
	end)
	if not success then
		warn("Filtering failed: " .. errorMessage)
	end
	return filteredText, filteredText==text -- I also need to know if it has been filtered so that I can avoid displaying #s
end

Thank you for helping.
Louis.

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