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

How would I go about doing a Kick Player chat command?

Asked by 4 years ago

Hello! Basically I want to have " :kick (player name) (reason) ", however, I cannot figure out how I could get the Player Name and the Reason as separate strings.

I've got something like:

game.Players.PlayerAdded:connect(function(p)
    p.Chatted:connect(function(c)
        if string.sub(c,1,5) == ":kick" then
            -- Need help on this bit, cheers!
        end
    end)
end)

Cheers,

3 answers

Log in to vote
1
Answered by
KoruZX 35
4 years ago
Edited 4 years ago
game.Players.PlayerAdded:connect(function(p)
    p.Chatted:connect(function(c)
        if string.sub(c,1,5) == ":kick" then
          local raw = string.split(c, " ")
          local targetPlayerName = raw[2]
          local reason = raw[3] == nil and "Unspecified reason" or raw[3]
          if game.Players:FindFirstChild(targetPlayerName) then
            game.Players:FindFirstChild(targetPlayerName):Kick(reason)
        end
        end
    end)
end)

Ad
Log in to vote
1
Answered by
Nanomatics 1160 Moderation Voter
4 years ago
Edited 4 years ago

You'll find string.split() very useful in this case, which returns a table with the strings separated by the character you choose. More on this here.

I put it together for you, didn't do the kicking part for you though, I'm guessing you're capable of doing that.

game.Players.PlayerAdded:connect(function(p)
    p.Chatted:connect(function(c)
        if string.sub(c,1,5) == ":kick" then
            local Separator = string.split(string.sub(c, 6), " ")
            local playerName = Separator[2]:lower() -- gets the player name
            local msg = Separator[3] -- gets the last bit of text
            local plrObj 
            for i, v in pairs(game.Players:GetPlayers()) do -- match playername w player
                if string.match(string.lower(v.Name), playerName) then
                    plrObj = v
                    plrObj:Kick(msg)
                end
            end
            -- Need help on this bit, cheers!
        end
    end)
end)

Hope I helped. If you have any questions please let me know.

Log in to vote
0
Answered by
ImTrev 344 Moderation Voter
4 years ago
Edited 4 years ago

Something I whipped up real fast. Would have to do some more tinkering if wanted a "spaced out" message.

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:connect(function(msg)
        local newMsg = msg:split(" ")
        if newMsg[1] == ":kick" then
            game.Players[newMsg[2]]:Kick(newMsg[3])
        end
    end)
end)

Answer this question