Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How do I make chat commands?

Asked by
Bubby4j 231 Moderation Voter
11 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 — 11y

3 answers

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
11 years ago

For a general format:

01game.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)
15end)

Command arguments are separated by /s

Ad
Log in to vote
1
Answered by
Bubby4j 231 Moderation Voter
11 years ago

You bind to the .Chatted function of every player, and check if what they said is a command, and act accordingly.

01game.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)
10end)
Log in to vote
-2
Answered by 10 years ago

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

Answer this question