3

# How do I make chat commands?

Bubby4j 231
8 years ago

I want to add chat commands to my game, how do I script them?

0
Yes, I know how to do this, I created this question for completeness. Bubby4j 231 — 8y

1
1waffle1 2908
8 years ago

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
end
end)
end)


Command arguments are separated by /s

1
Bubby4j 231
8 years ago

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)

-2