Classe do mecanismo
TextChatService
*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
ChatVersion
Métodos
CanUserChatAsync
CanUsersChatAsync
CanUsersDirectChatAsync
Devolução
Amostras de código
PodeUsuariosConversarDiretamenteAsync
local TextChatService = game:GetService("TextChatService")
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 nilDisplayBubble
GetChatGroupsAsync
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 backend.
Exemplo de estrutura:
ActiveServers = {
{
serverId = "abc",
accessCode = "código-de-servidor-reservado",
groupUserCounts = {
groupA = 12,
groupB = 5
}
},
{
serverId = "def",
accessCode = "código-de-servidor-reservado",
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 que 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()
TeleportService:TeleportToPrivateServer(
TARGET_PLACE_ID,
bestServer.accessCode,
{ player }
)
end)
if not ok then
warn("Teletransporte falhou:", err)
end
end
Players.PlayerAdded:Connect(teleportPlayerToBestServer)Eventos
BubbleDisplayed
TextChatService.BubbleDisplayed(
Parâmetros
MessageReceived
Parâmetros
SendingMessage
Parâmetros
Callbacks
OnBubbleAdded
Parâmetros
Devolução
OnChatWindowAdded
Parâmetros
Devolução
OnIncomingMessage
Parâmetros
Devolução