How ban a player with chatted?

Hi, I would like to create a player ban command, how can I do it?

I would like to write in the chat → /Ban playerName, this works with any player except a specific player id, how can I do that?

Example:
/Ban playerNameI want to ban the player except a specific player id.

I’m not sure how to do it, could someone give me a hand? I would appreciate it very much.

Script:

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)
    -- do stuff with msg and player
    if msg == "/Ban ".. player.Name then
    -- ban the player except specific player id.
end
end)
end)

You will need to study DataStores and when you ban them with a command, it will set a value in the DataStore under the player’s key that he/she is banned. So when they join the game, if you find that they are a banned player, you automatically kick them from the server.

DataStores!

The easiest way to handle this is by using a DataStore which would be able to save the Player’s Ban, in your instance we’re wanting to “ban” a Player when we chat a specific command

We’d need to first get all of the Players that are currently in the server, then detect if that specific Player is equal to what we want to search for here:

    Player.Chatted:Connect(function(Msg)
        for _, Target in pairs(game.Players:GetPlayers()) do
            if msg == "/Ban "..Target.UserId then --This will check for all players, and if the player we're looking for is valid

            end
        end
    end)

The next thing, is that there are 2 functions that should hopefully help us with this Ban Command:

  • SetAsync, which will save that specific Player’s Ban (With a provided key)

  • GetAsync, which would load that Player’s Ban if they have one (Given a provided key)

What we could possibly do then, is upon when a player first joins the game, is go ahead & check if there’s a valid “Ban key” for the player that joined

local DataService = game:GetService("DataStoreService")
local DataName = DataService:GetDataStore("Bans")

game.Players.PlayerAdded:Connect(function(Player)
    local BanCheck
    local success, whoops = pcall(function()
        BanCheck = DataName:GetAsync(Player.UserId) 
    end)

    if success and BanCheck then --If both results outputted as true, then we can ban this player >:(
        Player:Kick("You have been banned from this game, reason: MEANIE")
    end
end)

We’d always want to encase our DataStore functions in pcalls so that they won’t error & break the script, this would return back as a “warning” or a “success” if what we’re checking for is valid or not

Back to our looping script, now this is where we’d want to call SetAsync() to save the Player’s Ban ID to prevent them from rejoining

    Player.Chatted:Connect(function(Msg)
        for _, Target in pairs(game.Players:GetPlayers()) do
            if msg == "/Ban "..Target.UserId then --This will check for all players, and if the player we're looking for is valid
                local success, whoops = pcall(function()
                    DataName:SetAsync(Target.UserId) 
                end)

                if success then
                    print(Target.Name.." has been added to the ban list!")
                else
                    warn("An error occurred while trying to save this Player's Ban: ", whoops)
                end
                Player:Kick("You have been banned from this game, reason: You put pineapple on pizza")
            end
        end
    end)

SetAsync would set that specific Player’s UserId as a key, and that once they rejoin they’ll be banned

Hopefully, our full code should look like this!

local DataService = game:GetService("DataStoreService")
local DataName = DataService:GetDataStore("Bans")

game.Players.PlayerAdded:Connect(function(Player)
    local BanCheck
    local success, whoops = pcall(function()
        BanCheck = DataName:GetAsync(Player.UserId) 
    end)

    if success and BanCheck then --If both results outputted as true, then we can ban this player >:(
        Player:Kick("You have been banned from this game, reason: MEANIE")
    end

    Player.Chatted:Connect(function(Msg)
        for _, Target in pairs(game.Players:GetPlayers()) do
            if msg == "/Ban "..Target.UserId then --This will check for all players, and if the player we're looking for is valid
                local success, whoops = pcall(function()
                    DataName:SetAsync(Target.UserId) 
                end)

                if success then
                    print(Target.Name.." has been added to the ban list!")
                else
                    warn("An error occurred while trying to save this Player's Ban: ", whoops)
                end
                Player:Kick("You have been banned from this game, reason: You put pineapple on pizza")
            end
        end
    end)

end)
2 Likes

Thank you very much for your help!! I appreciate it very much :smiley: :smiley:

1 Like

Careful!

This code snippet would let anyone run the ban command!

I advise including a system to only listen to a players chat if they are an admin; that way only admins can ban users!

Wouldn’t be good if a normal player banned the owner!

Here’s an example of the code snippet @JackscarIitt provided; but with the added “Are they an admin?” check. Albeit simple, this would also allow admins to ban other admins! That and admins can ban themselves!

I invite you to experiment with this and find a good way to fit it with all of your needs.

local DataService = game:GetService("DataStoreService")
local DataName = DataService:GetDataStore("Bans")

--// UserIds of users who are admins!
--// The owner of the game is automatically an admin!
local Admins = {
    game.CreatorId
}

local function IsAdmin(Player)
   return table.find(Admins, Player.UserId) ~= nil --// Return a boolean
end

game.Players.PlayerAdded:Connect(function(Player)
    local BanCheck
    local success, whoops = pcall(function()
        BanCheck = DataName:GetAsync(Player.UserId) 
    end)

    if success and BanCheck then --If both results outputted as true, then we can ban this player >:(
        Player:Kick("You have been banned from this game, reason: MEANIE")
        return
    end

    if IsAdmin(Player) then
        Player.Chatted:Connect(function(Msg)
            for _, Target in pairs(game.Players:GetPlayers()) do
                if msg == "/Ban "..Target.UserId then --This will check for all players, and if the player we're looking for is valid
                    local success, whoops = pcall(function()
                        DataName:SetAsync(Target.UserId) 
                    end)

                    if success then
                        print(Target.Name.." has been added to the ban list!")
                    else
                        warn("An error occurred while trying to save this Player's Ban: ", whoops)
                    end

                    Player:Kick("You have been banned from this game, reason: You put pineapple on pizza")
                end
            end
        end)
    end
end)
3 Likes