此範例演示如何根據玩家在 群組 中的成員身份來指定 聊天標籤。聊天標籤是一種用於在聊天窗口中可視化識別玩家的方式,對於指示玩家的角色或狀態非常有用。

由於 文本聊天回調 期望非延遲回調,因此在 TextChatService.OnIncomingMessage 回調中查詢玩家的群組成員身份狀態並不推薦,因為這可能導致聊天系統掛起或變得無響應。
相反,當玩家加入伺服器時設置玩家的 屬性。設置屬性可以讓您在遊戲的其他部分重用玩家的狀態,例如允許訪問特定區域或提供額外體驗。
在 ServerScriptService 中創建一個 Script,並添加以下代碼:
伺服器local Players = game:GetService("Players")-- 將 123456 替換為您要檢查的群組 IDlocal 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)在 StarterPlayer ⟩ StarterCharacterScripts 中創建一個 LocalScript,並添加以下代碼以在聊天窗口中顯示 [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