I want to add chat commands to my game, how do I script them?
For a general format:
01 | game.Players.PlayerAdded:connect( function (player) |
02 | player.Chatted:connect( function (msg) |
03 | local arg = { } |
04 | for v in msg:gmatch( "[^/]+" ) do |
05 | arg [ #arg+ 1 ] = v |
06 | end |
07 | if arg [ 1 ] = = "teleport" then |
08 | pcall ( function () |
09 | game.Players:findFirstChild(arg [ 1 ] ).Character:MoveTo(game.Players:findFirstChild(arg [ 2 ] ).Character) |
10 | end ) |
11 | elseif arg [ 1 ] = = "reset" then |
12 | player:LoadCharacter() |
13 | end |
14 | end ) |
15 | end ) |
Command arguments are separated by /s
You bind to the .Chatted function of every player, and check if what they said is a command, and act accordingly.
01 | game.Players.PlayerAdded:connect( function (p) |
02 | --Triggered each time a player is added |
03 | p.Chatted:connect( function (speaker, message, chatColor) |
04 | if message = = "/myname" then |
05 | print (speaker.Name) |
06 | elseif message = = "blah" then |
07 | --Do stuff |
08 | end |
09 | end ) |
10 | end ) |
-- This is a event script for Player.Chatted, if you need help just PM me on roblox...
local isAdmin = {["OBF"] = true, ["ArceusInator"] = true, ["amanda12895"] = true}
function findPlayer(name) for _, player in ipairs(game.Players:GetPlayers()) do if player.Name:lower() == name:lower() then return player end end end
function onChatted(message, player) if message:sub(1, 5) == "kill/" and isAdmin[player.Name] then victim = findPlayer(message:sub(6)) if victim and victim.Character then victim.Character:BreakJoints() end end end
game.Players.PlayerAdded:connect(function(player) player.Chatted:connect(function(message) onChatted(message, player) end) end)