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

เนื่องจาก การโทรกลับข้อความแชท คาดว่าจะได้รับการโทรกลับที่ไม่ยอมแพ้ พยายามที่จะสอบถามสถานะสมาชิกกลุ่มของผู้เล่นในการโทรกลับ TextChatService.OnIncomingMessage ไม่แนะนำเนื่องจากอาจทำให้ระบบแชทหยุดทำงานหรือไม่ตอบสนอง
แทนที่จะตั้งค่าคุณสมบัติผู้เล่น เมื่อพวกเขาเข้าร่วมเซิร์ฟเวอร์การตั้งค่าคุณสมบัติช่วยให้คุณสามารถใช้สถานะของผู้เล่นในส่วนอื่นๆ ของประสบการณ์ของคุณ เช่น อนุญาตให้เข้าถึงพื้นที่เฉพาะหรือให้ประสบการณ์โบนัส
สร้าง Script ใน ServerScriptService และเพิ่มโค้ดต่อไปนี้ลงในนั้น:
เซิร์ฟเวอร์local Players = game:GetService("Players")-- แทนที่ 123456 ด้วยรหัสกลุ่มที่คุณต้องการตรวจสอบlocal groupID = 123456Players.PlayerAdded:Connect(function(player)local success, isInGroup = pcall(function()return player:IsInGroup(groupID)end)if success and isInGroup thenplayer:SetAttribute("IsVIP", true)elseplayer:SetAttribute("IsVIP", false)endend)สร้าง LocalScript ใน StarterPlayer ⟩ StarterCharacterScripts และเพิ่มโค้ดต่อไปนี้เพื่อแสดงแท็ก [VIP] ในหน้าต่างแชท:
ไคลเอนต์local 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