I want to add chat commands to my game, how do I script them?
For a general format:
game.Players.PlayerAdded:connect(function(player) player.Chatted:connect(function(msg) local arg={} for v in msg:gmatch("[^/]+")do arg[#arg+1]=v end if arg[1]=="teleport"then pcall(function() game.Players:findFirstChild(arg[1]).Character:MoveTo(game.Players:findFirstChild(arg[2]).Character) end) elseif arg[1]=="reset"then player:LoadCharacter() end end) 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.
game.Players.PlayerAdded:connect(function(p) --Triggered each time a player is added p.Chatted:connect(function(speaker, message, chatColor) if message == "/myname" then print(speaker.Name) elseif message == "blah" then --Do stuff end end) 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)