这个例子展示了如何根据玩家在 组中的会员身份 分配 聊天标签。聊天标签是一种视觉识别聊天窗口中的玩家的方法,可用于指示玩家的角色或状态。

因为 文本聊天回调 期望非妥协回调,尝试查询玩家在 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