TextChatService 具有用于常见目的的内置聊天命令,例如静音其他玩家和使用角色表情。您可以通过在 Studio 的 属性 窗口中将 CreateDefaultCommands 设置为 true 来启用这些命令。
您还可以使用 TextChatCommand 添加自定义命令。当用户在聊天输入框中发送预定义命令时,会触发由 TextChatCommand.Triggered 定义的回调以执行您的自定义操作。
以下示例展示了如何创建一个聊天命令,允许玩家在输入 /super 或 /mini 时增加或减少他们角色的大小。
在 TextChatService 内添加一个 TextChatCommand 实例。
将其重命名为 SizeCommand。

将其 PrimaryAlias 属性设置为 /super,将其 SecondaryAlias 设置为 /mini。

在 ServerScriptService 中插入以下 Script 以定义聊天命令的回调,缩放角色的大小:
Scriptlocal TextChatService = game:GetService("TextChatService")local Players = game:GetService("Players")local sizeCommand: TextChatCommand = TextChatService:WaitForChild("SizeCommand")sizeCommand.Triggered:Connect(function(textSource, message)local scaleMult = 1local messageWords = string.split(message, " ")if messageWords[1] == "/super" thenscaleMult = 2elseif messageWords[1] == "/mini" thenscaleMult = 0.5endlocal player = Players:GetPlayerByUserId(textSource.UserId)if player thenlocal character = player.Characterif character thenlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenfor _, child in humanoid:GetChildren() doif child:IsA("NumberValue") thenchild.Value *= scaleMultendendendendendend)