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

How would i unban a player if they werent in the game?

Asked by
Zrxiis 26
6 years ago

Im trying to make a script so i can ban, kick, and unban players. But I'm not able to unban if the players arent in the game.

local admins = {
    "FangBlade16";
}

local banlist = game:GetService("DataStoreService"):GetDataStore("BanList")

game.Players.PlayerAdded:connect(function(player)
    local playerId = player.UserId
    local check = banlist:GetAsync(playerId)
    if check == nil then
        banlist:SetAsync(playerId,"Clear")
    elseif check == "Banned" then
        player:Kick("You are banned from this game")
    elseif check == "Clear" then
        print("You are clear!")
    end

    for i, admins in ipairs(admins) do
        if player.Name == admins then
            player.Chatted:connect(function(msg)
                for i,players in ipairs(game.Players:GetChildren()) do
                    if msg == ":kick "..players.Name then
                        players:Kick("You have been kicked by: "..player.Name)
                    elseif msg == ":ban "..players.Name then
                        banlist:SetAsync(players.UserId,"Banned")
                        players:Kick("You have been banned by: "..player.Name)
--This part below wont work.
                    elseif msg == ":unban "..players.Name then
                        local pardonedId = game.Players:GetUserIdFromNameAsync(players.Name)
                        banlist:SetAsync(pardonedId,"Clear")

                    end
                end     
            end)
        end
    end
end)

1 answer

Log in to vote
1
Answered by 6 years ago
...

player.Chatted:connect(function(msg)
    if string.sub(msg, 1, 6) == ":unban" then
        local playerName = string.sub(msg, 8)
        -- first look for the player in the game
        for _, player in pairs(game.Players:GetPlayers()) do
            if player.Name == playerName then
                banlist:SetAsync(player.UserId, "Clear")
                return
            end
        end
        -- if we get to this line, it means the player isn't in this game
        local success, pardonedId = pcall(function()
            return game.Players:GetUserIdFromNameAsync(playerName)
        end)
        if success then
            banlist:SetAsync(pardonedId, "Clear")
        else
            print("User '"..playerName.."' does not exist")
        end
    else
        -- this is your existing code for :kick and :ban
        for _, player in pairs(game.Players:GetPlayers()) do
            if msg == ":kick "..player.Name then
                player:Kick("You have been kicked by: "..player.Name)
            elseif msg == ":ban "..player.Name then
                banlist:SetAsync(player.UserId,"Banned")
                player:Kick("You have been banned by: "..player.Name)
            end
        end   
    end  
end)

...
0
Oops I didn't realize that `player` was already defined as the chatting player, so it will be overwritten in the second half of your script block and the kick messages will tell the kicked players that they were kicked by themselves! If you want to fix this, rename the `player` variable from line 24 to the end to something else, like `suspect` or `troublemaker` WillieTehWierdo200 966 — 6y
0
Thank you it worked. Zrxiis 26 — 6y
Ad

Answer this question