這個例子顯示如何根據玩家在 群組 的會員資格分配 聊天標籤 給玩家。聊天標籤是一種視覺標示聊天窗口中的玩家的方式,並對指示玩家的角色或狀態有用。

因為 文字聊天回叫 期望非屈服回調,嘗試在 TextChatService.OnIncomingMessage 回叫中查詢玩家的群籍狀態可能會導致聊天系統掛機或無法回應,因此不建議在回叫中查詢玩家的群籍狀態。
取而代之,當玩家加入伺服器時,設置 玩家屬性。設定特性讓你在體驗的其他部分重複使用玩家的狀態,例如允許進入特定區域或提供額外體驗。
在 Script 中創建一個 ServerScriptService 並將以下代碼添加到它:
服務器local Players = game:GetService("Players")-- 將 123456 替換為您想要檢查的群組 IDlocal 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