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

How do i save the players that are banned from the game?

Asked by 3 years ago
Edited 3 years ago
local DatastoreService = game:GetService("DataStoreService")
local ds = DatastoreService:GetDataStore("BanList")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event1 = ReplicatedStorage.AdminGui:WaitForChild("Ban")
local event2 = ReplicatedStorage.AdminGui:WaitForChild("Unban")

local Players = game:GetService("Players")

local bannedpeople = {}

ds:GetAsync(bannedpeople)

game.Players.PlayerAdded:Connect(function(plr)
    if table.find(bannedpeople, plr.UserId) then
        wait(0.2)
        plr:Kick("You are permanently banned from the game.")
        print("autoban")
    end
end)

local function Banned(admin, username, reason)
    local bannedplayer = Players:FindFirstChild(username)
    if bannedplayer ~= nil then
        game.Players:FindFirstChild(bannedplayer):Kick(reason)
        table.insert(bannedpeople, bannedplayer.UserId)
        ds:SetAsync(bannedpeople)
        print("banned")
    end
end

local function Unbanned(admin, username, reason)
    local unbannedplayer = Players:FindFirstChild(username)
    if unbannedplayer ~= nil then
        if table.find(bannedpeople, unbannedplayer.UserId)then
            table.remove(bannedpeople, table.find(bannedpeople, unbannedplayer.UserId))
            ds:SetAsync(bannedpeople)
            print("unbanned")
        end
    end
end

event1.OnServerEvent:Connect(Banned)
event2.OnServerEvent:Connect(Unbanned)

This question got edited, i wanna know if it's correct.

Please help me.

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

I'm really out of time, wrote this basic script which I am 100% will help, but basically, use only ONE key for the banned players, and also some code shortening using table.find:

local dss = game:GetService("DataStoreService")
local http = game:GetService("HttpService")
local banStore = dss:GetDataStore("banStore")

local banned = {}

game.Players.PlayerAdded:Connect(function(player)

    pcall(function()

        banned = http:JSONDecode(banStore:GetAsync("banned"))

    end)

    if banned then

        if table.find(banned,player.Name) then

            player:Kick("banned lol")

        end

    end

end)

game.Players.PlayerRemoving:Connect(function(player)

    banStore:SetAsync("banned",http:JSONEncode(banned))

end)

local function ban(player)

    table.insert(banned,#banned+1,player.Name)

end

Hope this helped! (even though i didn't really explain anything) Edit: added a function so you know how to add banned players.

0
what is the purpose of using jsonencode? why not just store the normal table information? Gey4Jesus69 2705 — 3y
1
Datastores only accept numbers and strings, and JSONEncode converts tables to strings. Hence why. However, from what I know, some time in the past they made it so it automatically converted it to json strings if you attempted to save tables to a datastore, but I still do it for a habit. User#32819 0 — 3y
1
Thanks, I was looking for this. User#33317 0 — 3y
0
yeah i was wondering bc i save tables to datastores all the time, but i suppose converting them yourself might be more efficient Gey4Jesus69 2705 — 3y
0
congrats a whole json encode decode system greatneil80 2647 — 3y
Ad

Answer this question