TextChatService 拥有用于普通目的的内置聊天命令,例如静音其他玩家和使用虚拟形象表情。您可以在 Studio 的 CreateDefaultCommands 窗口中设置 true 为 **** 以启用它们。
您还可以使用 TextChatCommand 添加自定义命令。用户在聊天输入栏中发送定义的命令会触发由 TextChatCommand.Triggered 定义的回调执行自定义操作。
以下示例显示了如何创建一个聊天命令,允许玩家在输入 /super 或 /mini 时增加或减少角色的大小。
在 TextChatCommand 内添加一个 TextChatService 实例。
将其重命名为 尺寸命令 。
将其 PrimaryAlias 属性设置为 /super 并将其 SecondaryAlias 设置为 /mini 。
在 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 = 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)