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)