Questo esempio dimostra come assegnare tag chat ai giocatori in base alla loro appartenenza a un gruppo. I tag chat sono un modo per identificare visivamente un giocatore nella finestra di chat e sono utili per indicare il ruolo o lo stato di un giocatore.

Poiché i callback della chat testuale si aspettano un callback che non produca yield, tentare di interrogare lo stato di appartenenza a un gruppo di un giocatore nel callback TextChatService.OnIncomingMessage non è consigliato, poiché potrebbe causare il blocco del sistema di chat o rendere il sistema non reattivo.
Invece, imposta un attributo per un giocatore quando si unisce al server. Impostare un attributo ti consente di riutilizzare lo stato del giocatore in altre parti del tuo gioco, ad esempio consentendo l'accesso a determinate aree o fornendo esperienze bonus.
Crea un Script in ServerScriptService e aggiungi il seguente codice:
Serverlocal Players = game:GetService("Players")-- Sostituisci 123456 con l'ID del gruppo che vuoi controllarelocal groupID = 123456Players.PlayerAdded:Connect(function(player)local success, isInGroup = pcall(function()return player:IsInGroupAsync(groupID)end)if success and isInGroup thenplayer:SetAttribute("IsVIP", true)elseplayer:SetAttribute("IsVIP", false)endend)Crea un LocalScript in StarterPlayer ⟩ StarterCharacterScripts e aggiungi il seguente codice per visualizzare un tag [VIP] nella finestra di chat:
Clientlocal 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