自訂文字聊天指令

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

TextChatService 內建了聊天指令用於一般用途,例如靜音其他玩家和使用虛擬人偶表情。您可以在 Studio 的 CreateDefaultCommands 窗口中設置 true 為 **** 以啟用它們。

您也可以使用 TextChatCommand 添加自訂指令。使用者在聊天輸入欄中發送定義的指令會觸發由 TextChatCommand.Triggered 定義的回呼來執行自定义行動。

下面的例子顯示了如何創建一個聊天指令,允許玩家輸入 /super/mini 時增加或減少角色的大小。

  1. TextChatCommand 內添加一個 TextChatService 實例。

  2. 將其重命名為 尺寸指令

  3. 將其 PrimaryAlias 屬性設為 /super 並將其 SecondaryAlias 設為 /mini

  4. ServerScriptService 內插入以下 Script 來定義擴大角色尺寸的聊天指令的回呼:

    腳本

    local TextChatService = game:GetService("TextChatService")
    local Players = game:GetService("Players")
    local sizeCommand: TextChatCommand = TextChatService:WaitForChild("SizeCommand")
    sizeCommand.Triggered:Connect(function(textSource, message)
    local scaleMult = 1
    local messageWords = string.split(message, " ")
    if messageWords[1] == "/super" then
    scaleMult = 2
    elseif messageWords[1] == "/mini" then
    scaleMult = 0.5
    end
    local player = Players:GetPlayerByUserId(textSource.UserId)
    if player then
    local character = player.Character
    if character then
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
    for _, child in humanoid:GetChildren() do
    if child:IsA("NumberValue") then
    child.Value *= scaleMult
    end
    end
    end
    end
    end
    end)