指定聊天標籤

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

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

在聊天窗口中附加到用戶名的 VIP 聊天標籤。

由於 文本聊天回調 期望非延遲回調,因此在 TextChatService.OnIncomingMessage 回調中查詢玩家的群組成員身份狀態並不推薦,因為這可能導致聊天系統掛起或變得無響應。

相反,當玩家加入伺服器時設置玩家的 屬性。設置屬性可以讓您在遊戲的其他部分重用玩家的狀態,例如允許訪問特定區域或提供額外體驗。

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

    伺服器

    local Players = game:GetService("Players")
    -- 將 123456 替換為您要檢查的群組 ID
    local groupID = 123456
    Players.PlayerAdded:Connect(function(player)
    local success, isInGroup = pcall(function()
    return player:IsInGroupAsync(groupID)
    end)
    if success and isInGroup then
    player:SetAttribute("IsVIP", true)
    else
    player:SetAttribute("IsVIP", false)
    end
    end)
  2. StarterPlayerStarterCharacterScripts 中創建一個 LocalScript,並添加以下代碼以在聊天窗口中顯示 [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
©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。