要点
在 ServerScriptService 的 Script 里为每条命令创建一个 TextChatCommand,设置它的 PrimaryAlias(例如 "/kick"),并放进 TextChatService。它的 Triggered 事件会传入发送者的 TextSource 和完整的消息文本。用 Players:GetPlayerByUserId(textSource.UserId) 找到玩家,检查他的 user ID 是否在你的管理员列表中,然后把消息拆成单词并执行操作。
- 在服务端检查管理员列表。放在 LocalScript 里的检查可能被作弊者绕过。
- 检查 user ID 而不是名字:玩家可以改名,而 ID 永远不会变。
- 与命令匹配的消息不会显示在聊天里,所以命令不会刷屏打扰其他玩家。
聊天命令的工作方式
TextChatService 里的 TextChatCommand 会监听聊天消息。当一条消息以它的 PrimaryAlias 或 SecondaryAlias 开头,后面跟着空格或什么都没有时,Roblox 不会把这条消息发给其他玩家,而是触发 Triggered。与别名的比较不区分大小写。Roblox 的自定义命令示例在服务端 Script 中连接 Triggered,检查管理员身份也正应该放在那里。
| 在聊天中输入 | 会发生什么 |
|---|---|
/kick Name | 把这个玩家移出服务器。 |
/tp Name | 把你传送到他身边。/tp Name1 Name2 会把 Name1 送到 Name2 身边。 |
/speed Name 50 | 设置他的 WalkSpeed(0 到 200)。不写数字则恢复为 16。 |
/heal Name | 把他的生命值完全恢复。 |
/kill Name | 杀死他的角色。 |
名字的位置也可以写 me、all 或 others,而且只写名字开头就够了:/heal ro 会治疗所有用户名或显示名称以“ro”开头的玩家。
第 1 步:添加命令
- 确认游戏使用的是 TextChatService:在 Explorer 中选中 TextChatService,在 Properties 窗口查看 ChatVersion。如果旧场景里仍然是 LegacyChatService,Roblox 关于游戏内文字聊天的页面有一节介绍如何迁移。
- 在 ServerScriptService 中插入一个名为
AdminCommands的 Script,粘贴下面的代码。 - 把
ADMINS中的123456换成你自己的 user ID。
-- ServerScriptService 中名为 AdminCommands 的 Script。
-- 供 ADMINS 中的 user ID 使用的聊天命令:/kick、/tp、/speed、/heal、/kill。
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TextChatService = game:GetService("TextChatService")
-- 你的 user ID 就是个人资料链接里的数字:roblox.com/users/123456/profile
local ADMINS: { [number]: boolean } = {
[123456] = true,
}
local function isAdmin(userId: number): boolean
-- 在你个人拥有的场景里进行 Studio 试玩测试时,你也算作管理员
local ownerInStudio = RunService:IsStudio() and game.CreatorType == Enum.CreatorType.User and userId == game.CreatorId
return ADMINS[userId] == true or ownerInStudio
end
-- 按用户名或显示名称的开头查找玩家,也支持 "me"、"all"、"others"
local function findPlayers(text: string?, caller: Player): { Player }
local found: { Player } = {}
if not text or text == "" or text == "me" then
table.insert(found, caller)
return found
end
local lower = string.lower(text)
for _, player in Players:GetPlayers() do
local name = string.lower(player.Name)
local display = string.lower(player.DisplayName)
if lower == "all"
or (lower == "others" and player ~= caller)
or string.sub(name, 1, #lower) == lower
or string.sub(display, 1, #lower) == lower
then
table.insert(found, player)
end
end
return found
end
local function getHumanoid(player: Player): Humanoid?
local character = player.Character
return character and character:FindFirstChildOfClass("Humanoid")
end
type Handler = (caller: Player, args: { string }) -> ()
local commands: { [string]: Handler } = {
kick = function(caller, args)
for _, target in findPlayers(args[2], caller) do
if target ~= caller then
target:Kick("Kicked by an admin")
end
end
end,
tp = function(caller, args)
-- /tp name:传送到他身边。/tp name1 name2:把 name1 送到 name2 身边。
local destination = if args[3] then findPlayers(args[3], caller)[1] else findPlayers(args[2], caller)[1]
local movers: { Player } = if args[3] then findPlayers(args[2], caller) else { caller }
local destCharacter = destination and destination.Character
if not destCharacter then
return
end
for _, mover in movers do
local character = mover.Character
if character and mover ~= destination then
character:PivotTo(destCharacter:GetPivot() * CFrame.new(0, 0, 4))
end
end
end,
speed = function(caller, args)
-- /speed name 50
local speed = tonumber(args[3]) or 16
for _, target in findPlayers(args[2], caller) do
local humanoid = getHumanoid(target)
if humanoid then
humanoid.WalkSpeed = math.clamp(speed, 0, 200)
end
end
end,
heal = function(caller, args)
for _, target in findPlayers(args[2], caller) do
local humanoid = getHumanoid(target)
if humanoid then
humanoid.Health = humanoid.MaxHealth
end
end
end,
kill = function(caller, args)
for _, target in findPlayers(args[2], caller) do
local humanoid = getHumanoid(target)
if humanoid then
humanoid.Health = 0
end
end
end,
}
-- 为每条命令创建一个 TextChatCommand。只有管理员能得到结果;对其他人来说
-- 命令什么都不做,消息也不会显示在聊天里。
for name, handler in commands do
local command = Instance.new("TextChatCommand")
command.Name = "Admin_" .. name
command.PrimaryAlias = "/" .. name
command.AutocompleteVisible = false -- 命令不会出现在其他玩家的自动补全提示中
command.Parent = TextChatService
command.Triggered:Connect(function(textSource: TextSource, unfilteredText: string)
local caller = Players:GetPlayerByUserId(textSource.UserId)
if not caller or not isAdmin(caller.UserId) then
return
end
handler(caller, string.split(unfilteredText, " "))
end)
end
commands 表为每个名字对应一个函数。底部的循环把每一项变成一个 TextChatCommand,所以新增一条命令只需要在表里加一项。string.split(unfilteredText, " ") 把消息拆成单词:args[1] 是命令,args[2] 是目标,args[3] 是数字或第二个目标。
AutocompleteVisible = false 会把命令从每个玩家输入斜杠时看到的提示中移除。管理员完整输入命令时,它仍然会生效。
第 2 步:添加你自己的命令
在 commands 表中添加一项。例如,设置跳跃力的命令 /jump Name 100:
- 复制
speed这一项,并把它重命名为jump。 - 在里面设置
humanoid.UseJumpPower = true和humanoid.JumpPower = math.clamp(power, 0, 300)。
如果要给所有人发消息,比如公告,请通过 RemoteEvent 发送,由每个客户端显示在屏幕上。凡是玩家能读到、又来自输入文本的内容,都必须先经过 Roblox 的文本过滤,所以公告请使用固定的消息,或用 TextService:FilterStringAsync 过滤。
测试
- 在你拥有的场景里点击 Play。在聊天中输入
/speed me 60:你会跑得更快,而聊天里不会出现这条消息。 - 摔伤之后输入
/heal me,确认生命值会恢复。 - 运行 2 名玩家的测试(Test 选项卡,Clients and Servers)。用 Player1 输入
/tp Player2。再用不是管理员的 Player2 输入/kill Player1:什么都不会发生。
常见错误与解决办法
如果命令没有任何效果,请先查看 Output 窗口和下面几项。
命令出现在聊天里,但什么都没发生
TextChatCommand 属于 TextChatService。请检查 TextChatService 的 ChatVersion 属性,并确认脚本是 ServerScriptService 中的 Script,且 Output 里没有红色错误。
在正式游戏中,命令对我没有效果
你的 user ID 不在 ADMINS 里。Studio 的例外只在 Studio 中有效,而且只适用于你个人账户(而不是群组)拥有的场景。
/tp 没有效果
两名玩家都必须有在世界中的角色。检查目标的名字:写用户名或显示名称的开头就够了,但有拼写错误就找不到任何人。
我想要等级,比如版主和所有者
把 ADMINS 改成让 user ID 对应一个等级数字,并为每条命令设置允许运行它的最低等级。
用 RoCode 构建
如果不想手动设置,RoCode 可以在你打开的场景里把它搭好。RoCode 是独立的 Roblox Studio AI 代理,并非 Roblox 出品:你在网页聊天里描述改动,它的 Studio 插件就会在你的 Explorer 中创建脚本。
为我和我的朋友(user ID 111 和 222)添加管理员命令:/kick、/tp、/speed、/heal、/kill 和 /fly,并在服务端检查。
- 编写命令脚本,包含你的管理员 ID 以及你描述的任何额外命令
- 可以先读取你的场景,沿用已有的内容,例如现有的聊天设置
- 它被要求用 Check Script 检查每个脚本能否编译,并在每组改动前后设置撤销点
在试玩测试中检查命令仍然需要你自己来做,而检查管理员身份需要第二名玩家。RoCode 如何连接到 Studio。
常见问题
如何给自己的 Roblox 游戏添加管理员命令?
在服务端 Script 中创建 TextChatCommand,并在每个 Triggered 处理函数里先按你的管理员列表检查发送者的 user ID,再执行任何操作。完整脚本见第 1 步。
需要 HD Admin 或 Adonis 吗?
不需要。Creator Store 里的管理员系统会加入很多命令和一个面板,但那是一大段不是你写的代码。一个简短的自写脚本只做你需要的事,不多也不少。
如何查看自己在 Roblox 的 user ID?
在 roblox.com 打开你的个人资料:链接中 /users/ 和 /profile 之间的数字就是你的 user ID。
玩家能在聊天里看到管理员命令吗?
不能。与 TextChatCommand 匹配的消息不会发送给其他玩家。
参考资料
- Roblox Creator Docs: TextChatCommand(英文). 别名、AutocompleteVisible 和 Triggered
- Roblox Creator Docs: Custom text chat commands(英文). 处理命令的服务端 Script
- Roblox Creator Docs: Player:Kick(英文). 带消息地移除玩家
- Roblox Creator Docs: Text filtering(英文). 何时需要过滤输入的文本
代码如何验证:本页的所有脚本都已用 Roblox API 类型定义(luau-lsp,2026年11月20日)通过 Luau strict 类型检查。类型检查能发现 API 名称写错和类型不匹配,但发现不了游戏逻辑上的错误。发布前请务必在 Studio 中测试。