Text Chat Service
Summary
Properties
Methods
CanUserChatAsync(userId: User):boolean |
CanUsersChatAsync(userIdFrom: User,userIdTo: User):boolean |
CanUsersDirectChatAsync(requesterUserId: User,userIds: {any}):{any} |
DisplayBubble(partOrCharacter: Instance,message: string):() |
GetChatGroupsAsync(players: Instances):{any} |
Events
BubbleDisplayed(partOrCharacter: Instance,textChatMessage: TextChatMessage):RBXScriptSignal |
MessageReceived(textChatMessage: TextChatMessage):RBXScriptSignal |
SendingMessage(textChatMessage: TextChatMessage):RBXScriptSignal |
Callbacks
OnBubbleAdded(message: TextChatMessage,adornee: Instance):Tuple |
OnChatWindowAdded(message: TextChatMessage):Tuple |
OnIncomingMessage(message: TextChatMessage):Tuple |
API Reference
Properties
Chat Version
Methods
Can User Chat Async
Can Users Chat Async
Can Users Direct Chat Async
Returns
Code Samples
CanUsersDirectChatAsync
local TextChatService = game:GetService("TextChatService")
-- Replace with the UserIds of the two users
local userId1 = 1
local userId2 = 2
local directChatParticipants = TextChatService:CanUsersDirectChatAsync(userId1, { userId2 })
-- Check for eligible participants
if #directChatParticipants > 0 then
local directChannel = Instance.new("TextChannel")
directChannel.Parent = TextChatService
for _, participant in directChatParticipants do
directChannel:AddUserAsync(participant)
end
return directChannel
end
warn("Could not create TextChannel. Not enough eligible users.")
return nilDisplay Bubble
Get Chat Groups Async
Parameters
players:Instances |
Returns
Code Samples
TeleportPlayerToBestChatServer
-- Server Script
local TextChatService = game:GetService("TextChatService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local TARGET_PLACE_ID = 1234567890 -- Destination place ID
--[[
Stubbed universe-wide server state.
In practice, this data could be stored and updated using MemoryStore
or another backend system.
Example shape:
ActiveServers = {
{
serverId = "abc",
privateServerId = game.PrivateServerId,
groupUserCounts = {
groupA = 12,
groupB = 5
}
},
{
serverId = "def",
privateServerId = game.PrivateServerId,
groupUserCounts = {
groupB = 8
}
}
}
]]
local ActiveServers = {}
local function teleportPlayerToBestServer(player)
local success, chatGroups = pcall(function()
return TextChatService:GetChatGroupsAsync({ player })
end)
if not success or not chatGroups then
warn("Failed to retrieve chat groups for player:", player.UserId)
return
end
-- Collect all chat group IDs the player is eligible for
local playerGroupIds = {}
for _, group in ipairs(chatGroups) do
for _, groupId in ipairs(group) do
playerGroupIds[groupId] = true
end
end
local bestServer = nil
local bestUserCount = 0
-- Choose the server with the highest number of compatible users
for _, server in ipairs(ActiveServers) do
local compatibleUserCount = 0
for groupId, userCount in pairs(server.groupUserCounts) do
if playerGroupIds[groupId] then
compatibleUserCount += userCount
end
end
if compatibleUserCount > bestUserCount then
bestUserCount = compatibleUserCount
bestServer = server
end
end
if not bestServer then
warn("No suitable server found for player:", player.UserId)
return
end
local ok, err = pcall(function()
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ReservedServerId = bestServer.privateServerId
TeleportService:TeleportAsync(
TARGET_PLACE_ID,
{ player },
teleportOptions
)
end)
if not ok then
warn("Teleport failed:", err)
end
end
Players.PlayerAdded:Connect(teleportPlayerToBestServer)Get Chat Groups Async
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local players = Players:GetPlayers()
local success, chatGroups = pcall(function()
return TextChatService:GetChatGroupsAsync(players)
end)
if not success then
warn("Failed to retrieve chat groups")
return
end
for _, group in ipairs(chatGroups) do
-- Each group contains chat group ID strings.
-- Players who share a matching group ID can chat with each other.
for _, groupId in ipairs(group) do
print("Chat group ID:", groupId)
end
endEvents
Bubble Displayed
Text Chat Service. Bubble Displayed(
Parameters
Message Received
Parameters
Sending Message
Parameters
Callbacks
On Bubble Added
Parameters
Returns
On Chat Window Added
Parameters
Returns
On Incoming Message
Parameters
Returns