Why my ban command ain't working?

So recently I’ve been wanting to do my own ban command script, it does kick players when I use the kick and ban command (Haven’t tested cross-servers yet), but it doesn’t “ban” the player.

Basically the ban command is working like the kick command but what I want it to do is banning the player permanently not just kick the player.

Chat Script:

local commands = {
"!kick",
"!ban",
"!unban"
}
local banModule = require(script.BanModule)
game.Players.PlayerAdded:Connect(function(player)
    local currentBanList = banModule:GetList() or {}
    if currentBanList[player.UserId] then
       print(#currentBanList)
       player:Kick(currentBanList[player.UserId])
    end
    -- Ban command
    if player.UserId == 1418534298 then
       player.Chatted:Connect(function(message)
           local splitUpCommand = string.split(message, " ")
           local theCommand = table.find(commands, splitUpCommand[1])
           if theCommand then
              local victim = splitUpCommand[2]
              if victim == "AstralBlu_e" or victim == "XAnimeX2" then return end
              local reason = string.sub(message, (#splitUpCommand[1] + #splitUpCommand[2] + 2 - #message))
              if splitUpCommand[1] == "!kick" then

                 banModule:kick(victim, reason)

              elseif splitUpCommand[1] == "!ban" then
                 banModule:ban(currentBanList, victim, reason)

              elseif splitUpCommand[1] == "!unban" then

                 banModule:unban(currentBanList, victim)
              end
           end
       end)
    end
end)

Module:

local banModule = {}
local DSS = game:GetService("DataStoreService")
local banData = DSS:GetDataStore("Ban_Data")
local messagingService = game:GetService("MessagingService")
local acrossServerBanKey = "ServerBanMessage"

local function messagingService(bannedPersonID, reason)
    local theCurrentReason
    local subConnect
    local thePlayer
    local messageSuccess = pcall(function()
          subConnect = messagingService:SubscribeAsync(acrossServerBanKey, function(message)
              local playerAndBanReasonTable = message.Data
              for i,v in pairs(playerAndBanReasonTable) do
                 thePlayer = game.Players:GetPlayerByUserId(i)
                 theCurrentReason = v
              end
          end)
      end)
      if messageSuccess then
         if thePlayer then
            thePlayer:Kick(theCurrentReason)
            subConnect:Disconnect()
         end
      end

      local publishMessageSuccess, publishErrorMessage = pcall(function()
          messagingService:PublishAsync(acrossServerBanKey, {[bannedPersonID] = reason})
      end)
      if not publishMessageSuccess then print(publishErrorMessage) end
end



function banModule:ban(currentBanList, victim, reason)
   local success, errormessage
   local t = 0
   local bannedPersonID = game.Players:GetUserIdFromNameAsync(victim)
   currentBanList[bannedPersonID] = reason
   if game.Players:GetPlayerByUserId(bannedPersonID) then
      game.Players:GetPlayerByUserId(bannedPersonID):Kick(reason)
   else
      messagingService(bannedPersonID, reason)
   end
   repeat
   success, errormessage = pcall(function()
       banData:SetAsync("BanList", currentBanList)
   end)
   if success then
      t = 3
      print("Successfully banned!")
      
   else
      t = t + 1
      warn(errormessage)
   end
   until t == 3
end
function banModule:unban(currentBanList, victim)
   local success, errormessage
   local t = 0
   currentBanList[game.Players:GetUserIdFromNameAsync(victim)] = nil
   repeat
      success, errormessage = pcall(function()
          banData:SetAsync("BanList", currentBanList)
      end)
      if success then
         t = 3
      else
        t = t + 1
        warn(errormessage)
      end
   until t == 3
end
function banModule:GetList()
   local success, errormessage
   local banList
   local t = 0
   repeat
   success, errormessage = pcall(function()
       banList = banData:GetAsync("BanList")
   end)
   if success then
      t = 3
      return banList
   else
      t = t + 1
      warn(errormessage)
   end
   until t == 3
end

function banModule:kick(victim, reason)
   local kickedPersonID = game.Players:GetUserIdFromNameAsync(victim)
   if game.Players:GetPlayerByUserId(kickedPersonID) then
      game.Players:GetPlayerByUserId(kickedPersonID):Kick(reason)
   else
      messagingService(kickedPersonID, reason)
   end
end
return banModule

1 Like

You’re not returning the list in module:GetList().

do return banList

1 Like

Actually I am, look again.

function banModule:GetList()
   local success, errormessage
   local banList
   local t = 0
   repeat
   success, errormessage = pcall(function()
       banList = banData:GetAsync("BanList")
   end)
   if success then
      t = 3
      return banList
   else
      t = t + 1
      warn(errormessage)
   end
   until t == 3
end

Still needing for help… someone help me pls.

Where do you run into any issues?

I dont really know, it always prints it saved successfully but I think it doesnt save the data or something is happening.

Update: I just checked printing the content of the table of banned players and it always prints that it didnt save, basically I added a print with table.unpack or “Not saved” and it always prints the second option.

And there are no errors.

Have you considered DataStore2? It is very efficient for stuff like this.
I am not saying you should, but you could probably give it a shot as it prevents data loss and has a caching system.

Add prints to see where the issue occurs.

Just checked and the problem occurs on the getlist, it always returns nil but the thing is it doesnt even warn any errors.

(I did consider Datastore2 but im trying to do at my own)

It should only return nil if there is no data saved.
Are you sure there are no typos?

Yeah im sure, it just aint returning anything.

I am considering I should use kohls admin or HD admin.

I’d give this a shot.

local banModule = {}

function banModule:GetList()
	local Tries = 0
	local Ok, Result
	
	repeat
		Tries += 1
		Ok, Result = pcall(function()
			BanData:SetAsync(Key)
		end)
	until Ok or Tries == 5
	
	if not Ok then
		print('Error saving data, ' .. Result)
	else
		return BanData
	end
end

It still doesnt work but it doesnt print it didnt error.

First, I see is that you’re doing SetAsync instead of GetAsync to get the banlist in :GetList().

Second, you shouldn’t GetList() each time someone joins, you might get ratelimited. You could instead do GetList() when someone joins and it has been atleast N seconds since the previous join.

Im not using SetAsync instead of GetAsync, I did replace the SetAsync to GetAsync on the code @Syclya gave, plus that the script doesnt even give the warning of throttle or anything so I dont believe that is the error.

And even if it had the throttle warning it would only wait 6 seconds to get the list.

To narrow down the problem, try outputting all the indexes and values after doing the ban command to see if it updated the dictionary, and outputting it again when a player joins to see if = banModule:GetList() or {} loaded it correctly
all your print()s are printing correctly, right?

Yes all my prints are printing correctly, also I already did a print like this. It simply prints a blank space.

When checking the dictionary after :SetAsync, since it’s blank, maybe the problem is with inserting (bannedPersonID, reason). Try printing reason in banModule:ban().

It does print the reason but it doesnt work…

I think I will use kohls admin instead.