分配聊天标签

*此内容使用人工智能(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