ตัวอย่างนี้แสดงวิธีการกำหนด แท็กแชท ให้กับผู้เล่นตามการเป็นสมาชิกใน กลุ่ม แท็กแชทเป็นวิธีการในการระบุผู้เล่นในหน้าต่างแชทและมีประโยชน์ในการระบุบทบาทหรือสถานะของผู้เล่น

เนื่องจาก การเรียกกลับแชทข้อความ คาดหวังการเรียกกลับที่ไม่ยอมให้พักการทำงาน การพยายามตรวจสอบสถานะการเป็นสมาชิกของกลุ่มของผู้เล่นใน TextChatService.OnIncomingMessage การเรียกกลับไม่แนะนำ เนื่องจากอาจทำให้ระบบแชทหยุดทำงานหรือไม่ตอบสนอง
แทนที่จะทำเช่นนั้น ให้ตั้งค่า แอตทริบิวต์ ของผู้เล่นเมื่อพวกเขาเข้าร่วมเซิร์ฟเวอร์ การตั้งค่าแอตทริบิวต์ทำให้คุณสามารถนำสถานะของผู้เล่นไปใช้ใหม่ในส่วนอื่น ๆ ของเกมของคุณ เช่น การอนุญาตให้เข้าถึงพื้นที่เฉพาะหรือให้ประสบการณ์โบนัส
สร้าง Script ใน ServerScriptService และเพิ่มโค้ดต่อไปนี้:
Serverlocal Players = game:GetService("Players")-- เปลี่ยน 123456 เป็น ID กลุ่มที่คุณต้องการตรวจสอบlocal groupID = 123456Players.PlayerAdded:Connect(function(player)local success, isInGroup = pcall(function()return player:IsInGroupAsync(groupID)end)if success and isInGroup thenplayer:SetAttribute("IsVIP", true)elseplayer:SetAttribute("IsVIP", false)endend)สร้าง LocalScript ใน StarterPlayer ⟩ StarterCharacterScripts และเพิ่มโค้ดต่อไปนี้เพื่อแสดงแท็ก [VIP] ในหน้าต่างแชท:
Clientlocal Players = game:GetService("Players")local TextChatService = game:GetService("TextChatService")TextChatService.OnIncomingMessage = function(message: TextChatMessage)local textSource = message.TextSourceif textSource thenlocal player = Players:GetPlayerByUserId(textSource.UserId)if player thenif player:GetAttribute("IsVIP") == true thenlocal overrideProperties = Instance.new("TextChatMessageProperties")overrideProperties.PrefixText = "[VIP] " .. message.PrefixTextreturn overridePropertiesendendendreturn nilend