Assegnare tag di chat

*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.

Questo esempio mostra come assegnare etichette di chat ai giocatori in base alla loro adesione a un gruppo.Le etichette di chat sono un modo per identificare visivamente un giocatore nella finestra di chat e utile per indicare il ruolo o lo Statodi un Giocatore.

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

Poiché le chiamate di chat di testo non cedenti si aspettano una chiamata non cedente, tentando di interrogare lo stato di appartenenza al gruppo di un giocatore nel RichiamaTextChatService.OnIncomingMessage non è raccomandato, poiché può causare il blocco del sistema di chat o diventare irrispondente.

Invece, imposta un attributo player quando si uniscono al Server.Impostare un attributo ti consente di riutilizzare lo stato del Giocatorein altre parti della tua esperienza, come consentire l'accesso a aree particolari o fornire esperienza bonus.

  1. Crea un Script in ServerScriptService e aggiungi il seguente codice ad esso:

    Server

    local Players = game:GetService("Players")
    -- Sostituisci 123456 con l'ID del gruppo che vuoi controllare
    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. Crea un LocalScript in StarterPlayerStarterCharacterScripts e aggiungi il seguente codice per visualizzare un tag [VIP] nella finestra di chat:

    Cliente

    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