Engine Class
TextChatService
*เนื้อหานี้แปลโดยใช้ 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
คุณสมบัติ
ChatVersion
วิธีการ
CanUserChatAsync
CanUsersChatAsync
CanUsersDirectChatAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
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 nilDisplayBubble
GetChatGroupsAsync
พารามิเตอร์
players:Instances |
ส่งค่ากลับ
ตัวอย่างโค้ด
ย้ายผู้เล่นไปยังเซิร์ฟเวอร์แชทที่ดีที่สุด
-- สคริปต์เซิร์ฟเวอร์
local TextChatService = game:GetService("TextChatService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local TARGET_PLACE_ID = 1234567890 -- รหัสสถานที่จุดหมาย
--[[
สถานะเซิร์ฟเวอร์ทั่วจักรวาลที่ Stubbed.
ในทางปฏิบัติ ข้อมูลนี้สามารถจัดเก็บและอัปเดตโดยใช้ MemoryStore
หรือระบบเบื้องหลังอื่น ๆ。
รูปแบบตัวอย่าง:
ActiveServers = {
{
serverId = "abc",
accessCode = "reserved-server-code",
groupUserCounts = {
groupA = 12,
groupB = 5
}
},
{
serverId = "def",
accessCode = "reserved-server-code",
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
-- รวบรวมกลุ่มแชททั้งหมดที่ผู้เล่นมีสิทธิ์เข้าได้
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()
TeleportService:TeleportToPrivateServer(
TARGET_PLACE_ID,
bestServer.accessCode,
{ player }
)
end)
if not ok then
warn("การย้ายล้มเหลว:", err)
end
end
Players.PlayerAdded:Connect(teleportPlayerToBestServer)เหตุการณ์
BubbleDisplayed
TextChatService.BubbleDisplayed(
พารามิเตอร์
MessageReceived
พารามิเตอร์
SendingMessage
พารามิเตอร์
Callbacks
OnBubbleAdded
พารามิเตอร์
ส่งค่ากลับ
OnChatWindowAdded
พารามิเตอร์
ส่งค่ากลับ
OnIncomingMessage
พารามิเตอร์
ส่งค่ากลับ