How would I save a list of players banned from a server?

Hey there so I am trying to make a list of players or a table of players that are banned from a server.
Script:

local bancommand = "!ban "
local banlist = {}

local function banFunc(name)
for _, player in pairs(game.Players:GetPlayers()) do
local targetedPlr = string.find(player.Name:lower(),name)

	if targetedPlr then
		table.insert(banlist, player.UserId)
		player:Kick("You have been banned!")
	end
end

end

game.Players.PlayerAdded:Connect(function(player)
if table.find(banlist,player.UserId) then
player:Kick(“You can’t join back because you aren banned.”)
end
end)

game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if string.sub(message,1,string.len(bancommand)):lower() == bancommand:lower() then
local UserName = string.sub(message,string.len(bancommand) + 1):lower()
banFunc(UserName)
end
end)
end)

1 Like

Could I (we) get more context on what you are troubled with? Do you want it saved temporarily, to the particular server alone, or permanently. Furthermore, is the code snip-it you provided working, or does not work and you need help fixing it?

1 Like

I need the table banlist to save so that if a player is banned and rejoins they will be on the ban list and get banned out of the game.

And the code you provided above does NOT work? Is there any particular part that simply isn’t working, or perhaps it is

That portion?

everything works its just that I don’t know how to make the table save.

Save the JSONEncoded table as a datastore value.

game:GetService("DataStoreService"):GetDataStore("BanLists"):SetAsync("First", game:GetService("HttpService"):JSONEncode(BAN_TABLE))

Um I tried doing something like that it didn’t work.

I’m actually making a system similar to this, and this is the easiest way to do this!

  • Create a bool in the player.
  • If the command is said, set the bool to true and save it.
  • When the player joins, check the bool, and if it’s true, kick them.

That’s the easiest way of doing it, and most efficient. Hope this helped!;

I will be releasing my system close to the end of the month, so stay tuned! :eyes:

I know the rest is easy besides saving it that’s why I made this post.

Attributes do not persist between sessions, the only way to do this would be as I said, with DataStoreService.

Plus that would be much more work.

I had mentioned using DataStoreService, however indirectly. And it’s actually way less work than trying to save a whole table! (Instead of editing the entire table, you can just edit their datastore.)

That’s a lot of requesting. When the server starts, you can load the entire table into memory and update it as you go instead of querying each player in the DataStore.

please help guys I don’t know also I tried using bool value but the value would save its just that if it was true it wouldn’t still kick player
script:

local dataStoreService = game:GetService(“DataStoreService”)
local dataStore = dataStoreService:GetDataStore(“dataStore”)

local bancommand = "!ban "

local function banFunc(name)
for _, player in pairs(game.Players:GetPlayers()) do
local targetedPlr = string.find(player.Name:lower(),name)

	if targetedPlr then
		player:FindFirstChild("bannedVal").Value = true
		player:Kick("You have been banned!")
	end
end

end

game.Players.PlayerAdded:Connect(function(player)
if player:FindFirstChild(“bannedVal”) == true then
print(“Banning again”)
player:Kick(“You can’t join back because you are banned.”)
print(“Banned”)
end
end)

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

player.Chatted:Connect(function(message)
	if string.sub(message,1,string.len(bancommand)):lower() == bancommand:lower() then
		local UserName = string.sub(message,string.len(bancommand) + 1):lower()
		banFunc(UserName)
		bannedVal.Value = true
	end
end)

local playerId = "Player_"..player.UserId

local data

local suc,err = pcall(function()
	data = dataStore:GetAsync(playerId)
end)

if suc then
	bannedVal.Value = data
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local playerId = “Player_”…player.UserId

local data = player:FindFirstChild("bannedVal").Value

local suc, err = pcall(function()
	dataStore:SetAsync(playerId,data)		
end)

if suc then
else
	warn(err)
end

end)

See if this works:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("dataStore")

local bancommand = "!ban "

function loadData()
	local data

	local success, err = pcall(function()
		data = dataStore:GetAsync("bannedPlayers")
	end)
	if success then
		if data == nil then
			data = {}
		end
		return data
	else
		print(err)
	end
end

function saveData(data)
	local loadedData = loadData()
	loadedData[#loadedData + 1] = data
	local success, err = pcall(function()
		dataStore:SetAsync("bannedPlayers", loadedData)
	end)
	if not success then
		print(err)
		return false
	else
		return true
	end
end

local function banFunc(name)
	local targetedPlr = game.Players:FindFirstChild(name)

	if targetedPlr then
		if saveData(targetedPlr.UserId) then
			targetedPlr:Kick("You have been banned!")
		else
			-- Put what you want to happen if the data could not be saved
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local loaded = loadData()
	if table.find(loaded, player.UserId) then
		print("Banning again")
		player:Kick("You can’t join back because you are banned.")
		print("Banned")
	else
		player.Chatted:Connect(function(message)
			if string.sub(message, 1, #bancommand):lower() == bancommand:lower() then
				local UserName = string.sub(message, #bancommand + 1, #message)
				banFunc(UserName)
			end
		end)
	end
end)

I haven’t tested it.

sigh It doesn’t but thanks for your help.

I have made the boolvalue save but the boolvalue it is true but it does kick the player for some reason.

What about it doesn’t work? Are there any errors?

I use the Trello API to ban a user, I find it more comfortable and easier to use.

You need to make use of DataStores if you want player bans to persist across multiple server instances.

I did the value saves but it doesn’t kick the player

(kicks player if a value is true)