กำหนดแท็กแชท

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ตัวอย่างนี้แสดงวิธีการกำหนดแท็กแชท ให้กับผู้เล่นตามการสมัครสมาชิกในกลุ่ม แท็กแชทเป็นวิธีที่จะระบุตัวผู้เล่นในหน้าต่างแชทได้อย่างเห็นได้ชัดและมีประโยชน์ในการระบุบทบาทหรือสถานะของผู้เล่น

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

เนื่องจาก การโทรกลับข้อความแชท คาดว่าจะได้รับการโทรกลับที่ไม่ยอมแพ้ พยายามที่จะสอบถามสถานะสมาชิกกลุ่มของผู้เล่นในการโทรกลับ TextChatService.OnIncomingMessage ไม่แนะนำเนื่องจากอาจทำให้ระบบแชทหยุดทำงานหรือไม่ตอบสนอง

แทนที่จะตั้งค่าคุณสมบัติผู้เล่น เมื่อพวกเขาเข้าร่วมเซิร์ฟเวอร์การตั้งค่าคุณสมบัติช่วยให้คุณสามารถใช้สถานะของผู้เล่นในส่วนอื่นๆ ของประสบการณ์ของคุณ เช่น อนุญาตให้เข้าถึงพื้นที่เฉพาะหรือให้ประสบการณ์โบนัส

  1. สร้าง Script ใน ServerScriptService และเพิ่มโค้ดต่อไปนี้ลงในนั้น:

    เซิร์ฟเวอร์

    local Players = game:GetService("Players")
    -- แทนที่ 123456 ด้วยรหัสกลุ่มที่คุณต้องการตรวจสอบ
    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