自定义文本聊天命令

*此内容使用人工智能(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)