Text Chat Service
概要
プロパティ
方法
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} |
イベント
BubbleDisplayed(partOrCharacter: Instance,textChatMessage: TextChatMessage):RBXScriptSignal |
MessageReceived(textChatMessage: TextChatMessage):RBXScriptSignal |
SendingMessage(textChatMessage: TextChatMessage):RBXScriptSignal |
コールバック
OnBubbleAdded(message: TextChatMessage,adornee: Instance):Tuple |
OnChatWindowAdded(message: TextChatMessage):Tuple |
OnIncomingMessage(message: TextChatMessage):Tuple |
APIリファレンス
プロパティ
Chat Version
方法
Can Users Chat Async
Can Users Direct Chat Async
戻り値
コードサンプル
CanUsersDirectChatAsync
local TextChatService = game:GetService("TextChatService")
local directChatParticipants = TextChatService:CanUsersDirectChatAsync(userId1, { userId2 })
-- 対象となる参加者を確認します
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("TextChannelを作成できませんでした。対象となるユーザーが不足しています。")
return nilDisplay Bubble
Get Chat Groups Async
パラメータ
players:Instances |
戻り値
コードサンプル
最適なチャットサーバーにプレイヤーをテレポート
-- サーバースクリプト
local TextChatService = game:GetService("TextChatService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local TARGET_PLACE_ID = 1234567890 -- 目的地のプレイスID
--[[
全宇宙にわたるサーバー状態のスタブ。
実際には、このデータはMemoryStore
または他のバックエンドシステムを使用して保存および更新できます。
例の形:
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("プレイヤーのチャットグループの取得に失敗しました:", player.UserId)
return
end
-- プレイヤーが資格のあるすべてのチャットグループIDを収集
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
-- 互換性のあるユーザー数が最も多いサーバーを選択
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("プレイヤーに適したサーバーが見つかりませんでした:", 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("テレポートに失敗しました:", err)
end
end
Players.PlayerAdded:Connect(teleportPlayerToBestServer)チャットグループを非同期で取得
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("チャットグループの取得に失敗しました")
return
end
for _, group in ipairs(chatGroups) do
-- 各グループにはチャットグループIDの文字列が含まれています。
-- 一致するグループIDを共有するプレイヤーはお互いにチャットできます。
for _, groupId in ipairs(group) do
print("チャットグループID:", groupId)
end
endイベント
Bubble Displayed
Text Chat Service. Bubble Displayed(
パラメータ
Message Received
パラメータ
Sending Message
パラメータ
コールバック
On Bubble Added
パラメータ
戻り値
On Chat Window Added
パラメータ
戻り値
On Incoming Message
パラメータ
戻り値