Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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.

5 comments

0

what is the purpose of using jsonencode? why not just store the normal table information?

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.

1

Thanks, I was looking for this.

0

yeah i was wondering bc i save tables to datastores all the time, but i suppose converting them yourself might be more efficient

0

congrats a whole json encode decode system

This discussion is preserved as it was. New answers, comments, and votes are closed.