Text Chat Service
*Este conteúdo é traduzido por IA (Beta) e pode conter erros. Para ver a página em inglês, clique aqui.
Resumo
Propriedades
Métodos
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} |
Eventos
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 |
Referência API
Propriedades
Chat Version
Métodos
Can User Chat Async
Can Users Chat Async
Can Users Direct Chat Async
Devolução
Amostras de código
CanUsersDirectChatAsync
local TextChatService = game:GetService("TextChatService")
-- Substitua pelos UserIds dos dois usuários
local userId1 = 1
local userId2 = 2
local directChatParticipants = TextChatService:CanUsersDirectChatAsync(userId1, { userId2 })
-- Verifique os participantes elegíveis
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("Não foi possível criar o TextChannel. Usuários elegíveis insuficientes.")
return nilDisplay Bubble
Get Chat Groups Async
Parâmetros
players:Instances |
Devolução
Amostras de código
TeletransportarJogadorParaMelhorServidorDeChat
-- Script do Servidor
local TextChatService = game:GetService("TextChatService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local TARGET_PLACE_ID = 1234567890 -- ID do lugar de destino
--[[
Estado do servidor em todo o universo.
Na prática, esses dados poderiam ser armazenados e atualizados usando MemoryStore
ou outro sistema de backend.
Exemplo de formato:
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("Falha ao recuperar grupos de chat para o jogador:", player.UserId)
return
end
-- Coletar todos os IDs de grupos de chat para os quais o jogador é elegível
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
-- Escolher o servidor com o maior número de usuários compatíveis
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("Nenhum servidor adequado encontrado para o jogador:", 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("Falha no teletransporte:", err)
end
end
Players.PlayerAdded:Connect(teleportPlayerToBestServer)Obter Grupos de Chat Assincronamente
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("Falha ao recuperar grupos de chat")
return
end
for _, group in ipairs(chatGroups) do
-- Cada grupo contém strings de ID de grupo de chat.
-- Jogadores que compartilham um ID de grupo correspondente podem conversar entre si.
for _, groupId in ipairs(group) do
print("ID do grupo de chat:", groupId)
end
endEventos
Bubble Displayed
Text Chat Service. Bubble Displayed(
Parâmetros
Message Received
Parâmetros
Sending Message
Parâmetros
Callbacks
On Bubble Added
Parâmetros
Devolução
On Chat Window Added
Parâmetros
Devolução
On Incoming Message
Parâmetros
Devolução