Help with Ban table

Hi there! :smile:

I made a script that adds the players UserId to a table if that player does a certain action, and any player within that table will be kicked from the game, and then the player UserId should save to the table, so next time the player re-joins, he will still be in that table, thus being kicked from the game, (aka a ban), I’m having trouble with making my table save.

I have tried data stores, but as I’m fairly new to scripting I’m still not sure what to do.

Any help to how exactly I can make my table save would be much appreciated. :grinning: :+1:

3 Likes

Well we do not use a table for this. (Unless you are absolutely certain that there will only ever be 1 instance of roblox server in your game)

We just store a value in the player’s data depicting whether they have been banned or not.

Could you explain more on what your saying?
I plan on making 3 places in my game.

Well, if this is a black list then it would make more sense, but if it’s not, then you can set up a ban system that works with BoolValues. This is really only useful for things like custom admin.

Basically, there’s going to be a BoolValue that’s called “IsBanned”, and if someone has a ban action done to them, then the “IsBanned” value will be equal to true, with Datastores you can make sure it saves, and when the person joins again, you can use a PlayerAdded Event to check whether the value is true or false in their player. It seems like you’re new, so I don’t think I would like to give any code currently, it wouldn’t be helpful to you or me. I just simply gave the design/logic behind it.

1 Like

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