Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
3

How do I make chat commands?

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

3 answers

Log in to vote
1
Answered by
1waffle1 2863 Trusted Badge of Merit Moderation Voter Community Moderator
6 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
            player:LoadCharacter()
        end
    end)
end)

Command arguments are separated by /s

Ad
Log in to vote
1
Answered by
Bubby4j 231 Moderation Voter
6 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)
Log in to vote
-2
Answered by 5 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