指派聊天標籤

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

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

VIP chat tag appended to user name in the chat window.

因為 文字聊天回叫 期望非屈服回調,嘗試在 TextChatService.OnIncomingMessage 回叫中查詢玩家的群籍狀態可能會導致聊天系統掛機或無法回應,因此不建議在回叫中查詢玩家的群籍狀態。

取而代之,當玩家加入伺服器時,設置 玩家屬性。設定特性讓你在體驗的其他部分重複使用玩家的狀態,例如允許進入特定區域或提供額外體驗。

  1. Script 中創建一個 ServerScriptService 並將以下代碼添加到它:

    服務器

    local Players = game:GetService("Players")
    -- 將 123456 替換為您想要檢查的群組 ID
    local groupID = 123456
    Players.PlayerAdded:Connect(function(player)
    local success, isInGroup = pcall(function()
    return player:IsInGroup(groupID)
    end)
    if success and isInGroup then
    player:SetAttribute("IsVIP", true)
    else
    player:SetAttribute("IsVIP", false)
    end
    end)
  2. LocalScript 中創建一个 StarterPlayerStarterCharacterScripts 並將以下代碼添加到聊天窗口中顯示 [VIP] 標籤:

    客戶

    local Players = game:GetService("Players")
    local TextChatService = game:GetService("TextChatService")
    TextChatService.OnIncomingMessage = function(message: TextChatMessage)
    local textSource = message.TextSource
    if textSource then
    local player = Players:GetPlayerByUserId(textSource.UserId)
    if player then
    if player:GetAttribute("IsVIP") == true then
    local overrideProperties = Instance.new("TextChatMessageProperties")
    overrideProperties.PrefixText = "[VIP] " .. message.PrefixText
    return overrideProperties
    end
    end
    end
    return nil
    end