Text Chat Service
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ข้อมูลสรุป
คุณสมบัติ
วิธีการ
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 |
Callbacks
OnBubbleAdded(message: TextChatMessage,adornee: Instance):Tuple |
OnChatWindowAdded(message: TextChatMessage):Tuple |
OnIncomingMessage(message: TextChatMessage):Tuple |
เอกสารอ้างอิงเกี่ยวกับ API
คุณสมบัติ
Chat Version
วิธีการ
Can User Chat Async
Can Users Chat Async
Can Users Direct Chat Async
ส่งค่ากลับ
ตัวอย่างโค้ด
CanUsersDirectChatAsync
local TextChatService = game:GetService("TextChatService")
-- แทนที่ด้วย UserIds ของผู้ใช้สองคน
local userId1 = 1
local userId2 = 2
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 -- รหัสสถานที่ปลายทาง
--[[
สถานะเซิร์ฟเวอร์ทั่วจักรวาลที่ถูกสร้างขึ้น.
ในทางปฏิบัติ ข้อมูลนี้สามารถจัดเก็บและอัปเดตโดยใช้ 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
พารามิเตอร์
Callbacks
On Bubble Added
พารามิเตอร์
ส่งค่ากลับ
On Chat Window Added
พารามิเตอร์
ส่งค่ากลับ
On Incoming Message
พารามิเตอร์
ส่งค่ากลับ