重點摘要
在 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 屬性,並確認 Script 是放在 ServerScriptService 中的 Script,Output 裡也沒有紅色錯誤。
在正式遊戲中,指令對我沒有作用
你的 user ID 不在 ADMINS 中。Studio 的例外只在 Studio 中有效,而且只適用於你個人帳號擁有的場景,群組擁有的不算。
/tp 沒有反應
兩位玩家都必須有角色在世界中。請檢查目標名稱:寫出名稱或顯示名稱的開頭就夠了,但打錯字就找不到任何人。
我想要階級,例如管理員和擁有者
把 ADMINS 改成讓 user ID 對應到階級數字,並替每個指令設定可以執行它的最低階級。
用 RoCode 來做
如果不想手動設定,RoCode 可以在你開啟的場景中幫你建立。RoCode 是獨立的 Roblox Studio AI 代理,不是 Roblox 推出的:你在網頁聊天中描述變更,它的 Studio 外掛程式就會在你的 Explorer 中建立 Script。
幫我和我朋友(user ID 111 和 222)加入管理員指令:/kick、/tp、/speed、/heal、/kill 和 /fly,並在伺服器上檢查。
- 撰寫指令 Script,填入你的管理員 ID,以及你描述的任何額外指令
- 可以先讀取你的場景,沿用已經有的東西,例如現有的聊天設定
- 它被指示用 Check Script 檢查每個 Script 能否編譯,並在每一批變更前後設下復原點
指令仍然需要你自己試玩測試,而且檢查管理員權限需要第二位玩家。RoCode 如何連接 Studio。
常見問題
要怎麼在自己的 Roblox 遊戲加入管理員指令?
在伺服器 Script 中建立 TextChatCommand,並在每個 Triggered 處理函式中,先對照管理員清單檢查傳送者的 user ID,再執行任何動作。完整 Script 請見步驟 1。
需要 HD Admin 或 Adonis 嗎?
不需要。Creator Store 上的管理員系統會加入許多指令和一個面板,但那是一大段不是你寫的程式碼。一段簡短的自製 Script 只做你需要的事,不多也不少。
要怎麼知道自己的 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(英文). 什麼時候需要過濾輸入的文字
程式碼如何驗證:本頁每一段 Script 都通過 Luau 嚴格型別檢查,並使用 Roblox API 定義(luau-lsp,2026年11月20日)。型別檢查能找出拼錯的 API 與錯誤的型別,但無法檢查遊戲邏輯,所以發布前請先在 Studio 中測試。