Engine Class
TextService
Summary
Methods
FilterAndTranslateStringAsync(stringToFilter: string,fromUserId: number,targetLocales: {any},textContext: Enum.TextFilterContext):TextFilterTranslatedResult |
FilterStringAsync(stringToFilter: string,fromUserId: number,textContext: Enum.TextFilterContext):TextFilterResult |
GetFamilyInfoAsync(assetId: ContentId):Dictionary |
GetTextBoundsAsync(params: GetTextBoundsParams):Vector2 |
GetTextSizeOffsetAsync(fontSize: number,font: Font):number |
API Reference
Methods
FilterAndTranslateStringAsync
FilterStringAsync
TextService:FilterStringAsync(
Parameters
| Default Value: "PrivateChat" |
Returns
Code Samples
Pet Name Filter Example
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextService = game:GetService("TextService")
local Players = game:GetService("Players")
local Remotes = Instance.new("Folder")
Remotes.Name = "PetNamingRemotes"
Remotes.Parent = ReplicatedStorage
local UserNamedPet = Instance.new("RemoteEvent")
UserNamedPet.Name = "UserNamedPet"
UserNamedPet.Parent = Remotes
local SendPetName = Instance.new("RemoteEvent")
SendPetName.Name = "SendPetName"
SendPetName.Parent = Remotes
local RequestAllPetNames = Instance.new("RemoteFunction")
RequestAllPetNames.Name = "RequestAllPetNames"
RequestAllPetNames.Parent = Remotes
local filterResults = {}
local function broadcastPetName(userId)
local filterResult = filterResults[userId]
if filterResult then
for _, player in pairs(Players:GetPlayers()) do
if player then
-- spawn a new thread so as to not yield the update
task.spawn(function()
-- grab the filtered string for this user
local toUserId = player.UserId
local filteredString = filterResult:GetNonChatStringForUserAsync(toUserId)
filteredString = filteredString or ""
SendPetName:FireClient(player, userId, filteredString)
end)
end
end
end
end
UserNamedPet.OnServerEvent:Connect(function(player, petName)
local fromUserId = player.UserId
-- pcall to catch errors
local success, result = pcall(function()
return TextService:FilterStringAsync(petName, fromUserId)
end)
if success then
filterResults[fromUserId] = result
broadcastPetName(fromUserId)
else
print("Could not filter pet name")
end
end)
RequestAllPetNames.OnServerInvoke = function(player)
local toUserId = player.UserId
local petNames = {}
-- go through filter results and filter the pet name to be sent
for fromUserId, filterResult in pairs(filterResults) do
local filteredString = filterResult:GetNonChatStringForUserAsync(toUserId)
filteredString = filteredString or ""
-- need to convert userId to string so it can't be sent via a remote function
petNames[tostring(fromUserId)] = filteredString
end
return petNames
end
Players.PlayerRemoving:Connect(function(oldPlayer)
local userId = oldPlayer.UserId
filterResults[userId] = nil
end)GetFamilyInfoAsync
Parameters
assetId:ContentId |
Returns
Code Samples
Get Font Family Information
local TextService = game:GetService("TextService")
local familyToCheck = "rbxasset://fonts/families/Arimo.json"
local success, info = pcall(function()
return TextService:GetFamilyInfoAsync(familyToCheck)
end)
if success then
print("Name of the font:", info.Name)
print("Faces:")
for _, face in info.Faces do
print("--------")
print("Name:", face.Name)
print("Weight:", face.Weight.Name)
print("Style:", face.Style.Name)
end
endGetTextBoundsAsync
Parameters
Returns
Code Samples
Measuring Text Size
local TextService = game:GetService("TextService")
-- Declare parameters
local params = Instance.new("GetTextBoundsParams")
params.Text = "Hello world!"
params.Font = Font.new("rbxasset://fonts/families/GrenzeGotisch.json", Enum.FontWeight.Thin)
params.Size = 20
params.Width = 200
local success, bounds = pcall(function()
return TextService:GetTextBoundsAsync(params)
end)
if success then
print(bounds)
endGetTextSize
Returns
Code Samples
Getting Text Size
local TextService = game:GetService("TextService")
local function getTextBounds()
local message = "Hello world!"
local size = Vector2.new(1, 1)
local bounds = TextService:GetTextSize(message, 12, "SourceSans", size)
return bounds
end
print(getTextBounds())GetTextSizeOffsetAsync
Returns
Code Samples
Get Text Size Offset
local TextService = game:GetService("TextService")
local label = script.Parent.TextLabel
-- Get offset adjusted for the player's preferred text size setting
local success, offset = pcall(function()
return TextService:GetTextSizeOffsetAsync(label.TextSize, label.FontFace)
end)
if success then
-- Adjust core height of label by adding the offset
label.Size = UDim2.fromOffset(label.Size.X.Offset, label.Size.Y.Offset + offset)
end