How Can I Add A Ban Reason To My Ban Table?

Now, I have just learnt DataStores last night and I was wondering how I can add a Ban Reason to my bans table.

local DSS = game:GetService("DataStoreService")
local BannedDS = DSS:GetDataStore("BannedDS")
local banned = {}

game.Players.PlayerAdded:Connect(function(p)
	if table.find(BannedDS:GetAsync("Bans"), p.UserId) then
		p:Kick('Banned :c')
	else
			table.insert(banned, p.UserId)
			BannedDS:SetAsync("Bans", banned)
	end
end)

I’ve been thinking of this for a while but idk how to do it :smiley:

maybe this code will help you

Code

p:Kick(‘Banned’…‘Reason’)

also add this –
local Reason = { “For Exploiting” , “You were rude with players” } etc.

How does that work though? Does it use the same index from another table?

When I tested it, I ended up getting the kick reason as: {}

Yes, they will be added to the ban list for testing purposes lmao.

But, how would I get a ban for each specific user?

Edit: Could I Just Use StringValues?

That means you need to make a variation , that will make a reason of ban for example

local WhyBanned1 = “For exploiting”
if game.Players.LocalPlayer.Character.Humanoid.Speed >= 30 then

game.Players.PlayerAdded:Connect(function(p)
----your code-----
p:Kicked(“Banned for”…‘Reason’)

1 Like

Orden was simply saying to have that code in there. Yes, he should have stated that he should put it at above the kick, but it’s fairly obvious to scripters what he meant.

1 Like

@Aiden_12114

You could, but if you want to use DataStores for this you would do something as below.

Remember the only reason I’m going the mile typing everything here is that I’ve never created a dataStore for bans myself, otherwise a rough outline’s always more than enough.

  -- might lack certain necessity, just pseudo-code

  local Players = game:GetService("Players")
  local HTTP = game:GetService("HttpService")
  local DataStoreService = game:GetService("DataStoreService")
  local BansDs = DataStoreService:GetDataStore("BanStore")  

    

  local function onPlayerAdded(player)
         
  --// this part would be done somewhere else (adding to the BanList)
  --local bannedIds = {451391631; 123134; 1421412;}
  --BansDs:SetAsync("Bans",HTTP:JSONEncode(bannedIds))
  --//

  -- here we get that list, convert it from Json to string

  local json = BansDs:GetAsync("Bans")
  print(json)
  local banList = HTTP:JSONDecode(json)
  print(banList[1]) -- would print 451391631
  
          if table.find(banList, player.UserId) then
             player:Kick("You are banned")
          else
             print("welcome to the game")
          end
  end 

  Players.PlayerAdded:Connect(onPlayerAdded)

For instance, I used the command bar to set the table to the DataStore.

note that JSONEncode is used because tables cannot be saved directly to a DataStore, use JSONEDecode to decode that into it’s original value.

Also when I do BansDs:GetAsync(“Bans”) I get data for a supposed-to-be unique key (here we aren’t saving data for every player, rather saving it impersonally) which is the table in Json format


Yeah it really is obvious, but let’s not forget not everyone is an experienced programmer on the forum!


@ORden_Test1 your second proposed solution doesn’t seem quite right, you can’t index something in a variable through quotes, in your case, just remove the quotes entirely as the value itself is a string.

And btw, the local player’s humanoid’s speed property is not replicated to the server, so this won’t work:

I believe you meant

game:GetService("Players").PlayerAdded:Connect(function(player))
     player:Kick("Banned for" .. Reason)
end)

Also note that to concatenate strings, there are 2 dots (…) not an ellipsis(…)
one more thing, when the PlayerAdded event fires the player is passed as a parameter to the anonymous function connected, so you would have to do

  Players.PlayerAdded:Connect(function(p)
  --// stuff here
  end)
3 Likes

As you also have DataStore Limitations to acknowledge, I do not recommend saving global tables especially if you want to attach reasons. You may end up having to ban a large amount of users and exceed the maximum amount of characters you can have for a value.

Typically for bans, they will be stored along with player data or exclusively against the player in games without much need for intricate or proper data structuring. For example, in your case, assuming you don’t have many or any uses for DataStores, you can attach this data to the player. Assume that no value means not banned.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local BannedDataStore = DataStoreService:GetDataStore("Bans")

local function playerAdded(player)
    local success, result = pcall(function ()
        return BannedDataStore:GetAsync(player.UserId)
    end)

    if success then
        if result then
            player:Kick("Banned for: " .. result)
        end
    else
        -- In case DataStore fails, don't allow user in, otherwise a banned
        -- player may be able to get in during a failure.
        player:Kick("Error loading bans, please rejoin")
    end
end)

Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
    playerAdded(player)
end

To add a reason, just call SetAsync with a player’s UserId and a reason.

BannedDataStore:SetAsync(player.UserId, "Exploiting")

To remove a ban, RemoveAsync with their UserId.

BannedDataStore:RemoveAsync(player.UserId)
6 Likes