Help with Ban table

Could you explain more on how to save that value and when they join back and then determine whether its true or false.

This is what I have currently done using a table.

local SaveData = game:GetService("DataStoreService"):GetDataStore("SaveBannedData")
local banned = { }

game.Players.PlayerAdded:Connect(function(player)
        while true do
        	wait(1)
        	if player does action then
        		table.insert(banned, player.UserId)
        	else
        		-- do nothing
        	end
        		
        	if table.find(banned, player.UserId) then
        		player:kick("you are banned")
        	else
        		-- do nothing
        	end
        end
end)

game.Players.PlayerRemoving:Connect(function(player)
        SaveData:SetAsync(player.UserId, banned)
        print("saved success")
end)
1 Like

As I said before, this is a ban system not a black list, meaning it will ban any player that performs a certain action, its not a black list to prevent certain players from entering the game.

You would need to use GlobalDataStores to do this, and then you would approach it this way.

local Players = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")
local BanStore = DataStoreService:GetDataStore("BanStore")

local bannedMessage = "You have been banned from this game"


function banPlayer(target)
	local userId = target:IsA("Player") and target.UserId or target
	
	local success, banned = pcall(function()
		return BanStore:UpdateAsync(userId, true)
	end)
	
	if (success) then
		if (target:IsA("Player")) then
			target:Kick(bannedMessage)
		end
	end
end

function isPlayerBanned(userId)
	local success, result = pcall(function()
		return BanStore:GetAsync(userId)
	end)
	
	if (success and result) then
		return true
	end
	
	return false
end


function PlayerAdded(player)
	if (isPlayerBanned(player.UserId)) then
		player:Kick(bannedMessage)
	end
end


for _, player in pairs(Players:GetPlayers()) do
	coroutine.wrap(PlayerAdded)(player)
end


Players.PlayerAdded:Connect(PlayerAdded)

How would this help? and how would I synchronise this with the player being banned for doing a certain action?

Insside of the ban function you could add another argument named “reason” and then use :SetAsync() but instead of (true) you could do ({ true, reason })

GlobalDataStores can be accessed on all servers, so when the data store updates and the player is in your game it would kick the player.

The function I wrote for you allows you to use either a player Instance or a user id when using the ban command

I have recently made a banDS program that may help you since it’s not Roblox based.
https://devforum.roblox.com/t/external-datastore-ban-system/1156400

2 Likes

sorry, This is all very confusing for me, to keep it simple I’ll give a different scenario, lets say for example if the player touches a part it should ban them, (not kick) meaning somehow we save there data (tables? data stores?) and they get kicked every time they try re-join. how would I do that?

You would need to save the player’s key into the datastore. Whenever a child is added to players, check if their key is in that DS

I tried doing that by saving there key/ UserId in a table and then saving that table to a datastore, but its not working.

You don’t save it into the table. Your key is the directory

So lets make a table of the players data:

local PlayerDataTable = {
    isBanned = false, -- Make sure to have this
    Inventory = {},
    -- Etc
}

Now if it is false that means the player is not banned, and if it is true then the player is banned.
So whenever a player Joins check there data to see if isBanned is true.

game:GetService("Players").PlayerAdded:Connect(function(player)
   -- any method to get the playersdata | DataStore2, ProfileService or your own
   -- Once you have their data
   if data.isBanned then player:Kick("You are banned") end -- If its true kick the player everytime they connect.
end)

Hope this helps.

1 Like
local Bans = {"Username", 0000000} -- add a username (string) or a userid (number)

function checkBan(player)
   for _,banned in pairs(Bans) do
      if type(banned) == "string" and player.Name == banned then
         return true
      elseif type(banned) == "number" and player.UserId == banned then
         return true
      end
   end
   return false
end

game:GetService("Players").PlayerAdded:Connect(function(player)
   if checkBan(player) == true then
      player:Kick("Banned")
   end
end)

Thanks, I will try this out, and let you know :grinning: :+1:

Using a username as a directory is a very bad idea

See my previous post, post#7 I think? I’m making a ban system, not a black list

U can just change your username and now u don’t get kicked anymore

Yeah I know, thats why I said its a bad idea.

I just showed it as an example, he can just add a userId.

As I said, your post doesnt help, because I’m not making a black list, I’m making a ban system.
I don’t want to exclude certain players, i want to make a system that will ban any player that does a certain action and then saving that ban data.

How would I save the PlayerDataTable?