Unban Function not working

I am currently making an admin panel. Of course, it includes a ban and unban button. However, when I attempt to unban a player (whom is in the table), it does remove and I also used a print statement - returns nil.

Script:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Ban.OnServerEvent:Connect(function(p, name, reason)
	if not table.find(BlacklistedPlayers, name) then
		table.insert(BlacklistedPlayers, name)
	end
	
	if game.Players:FindFirstChild(name) then
		local success, errorm = pcall(function()
			game.Players:FindFirstChild(name):Kick(reason)
		end)
		if success then
			Notify:FireClient(p, "Player successfully banned.")
			for i, v in pairs(BlacklistedPlayers) do
				print(v)
				print(i)
			end
		elseif errorm then
			Notify:FireClient(p, "Player unsuccessfully banned, please retry.")
		end
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	if table.find(BlacklistedPlayers, player.Name) then
		local success, notsuccess = pcall(function()
			player:Kick("You are banned.")
		end)
		if notsuccess then
			warn("There has been an error attempting to ban the player whom is in the ban list.")
		end
	elseif not table.find(BlacklistedPlayers, player.Name) then
		return 
	end
end)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Unban.OnServerEvent:Connect(function(p, name)
	if table.find(BlacklistedPlayers, name) then
		local success, errorm = pcall(function()
			local pos = table.find(BlacklistedPlayers, name)
			table.remove(BlacklistedPlayers, pos)
		end)
		print(table.find(BlacklistedPlayers, name))
		if success then
			Notify:FireClient(p, "Player successfully unbanned.")
		elseif errorm then
			Notify:FireClient(p, "Player unsuccessfully unbanned, please retry.")
		end
	end
end)

BlacklistedPlayers is a table, the second parameter on both function are strings, same with reason (for the ban script).

1 Like

This should not be the issue but why do you have the kick method thing in a pcall function? I don’t think there is any need for that at all.

Just to notify the player associated with the event whether the ban was success, or unsuccess. I know it’s unnecessary but eh.

I know what pcall functions are for but there is like 99.9% chance it will not fail compared to somthing like datastores which are higher chance to fail due to datastores yeilds so need a pcall function.

I removed the pcall function part. The issue still remains the same however.

Actually, I added another print statement inside the PlayerAdded function. It prints out the name apparently, not nil.

The weird thing is that, whenever I unban a player, it prints out nil (finds a certain value inside the table, returns nil if cannot be found). If, the banned player joins, it will print out both value and index.

This is a misuse of the pcall function. pcall should only be used when calling API methods/functions or doing things that could fail for a legitimately unpredictable reason.

I just said I removed it. My point is that, either one of the event is overriding the unban event.

Just to clarify - what is the actual output that you are getting from your code? Is your Notify remote firing correctly and the player (should) be getting unbanned? Would be useful to have this cleared up before I attempt to give an answer.

In the meanwhile, however, there’s two debugging steps of use:

  1. Print out the entire BlacklistedPlayers table and look for the information you are after manually.
  2. Print (typeof(name)) at the start of each of the functions. It is 99.9% a string in the first one, but you may be sending an Instance or something on accident in the Unban function. It is worth checking.

No errors that is showing off from the output. I have an alt account and I tested the script in the game.

When I ban the player, it should print out the value pos of the player.
When I unban the player, it should print out “nil” because the player is not in the table anymore.

Although, it doesn’t actually unban the player. I added a print statement inside PlayerAdded, and will check if the player is inside the table.

What happens if you unban a certain player whom is inside the table is that, once the (not actually unbanned) player joins, it just kicks you out again - meaning you’re still banned.

Sorry if I wasn’t being clear. I was referring to whether the Notify calls in the bottom were running. I have made a test case with this code however and can’t appear to replicate the issue. Check the types of your parameters in your function calls with typeof() (especially in the Unban function). I believe that this is the issue.

Testing code, incase if I changed it in some way that fixed the issue:

local module = {}

local BlacklistedPlayers = {}

function module.GetPlayer(name)
	local Player = {
		Kick = function()
			print("kicked player lol")
		end,
		Name = name
	}
	return Player
end

function module.PlayerAdded(player)
	if table.find(BlacklistedPlayers, player.Name) then
		local success, notsuccess = pcall(function()
			player:Kick("You are banned.")
		end)
		if notsuccess then
			warn("There has been an error attempting to ban the player whom is in the ban list.")
		end
	elseif not table.find(BlacklistedPlayers, player.Name) then
		print("not kicking")
		return 
	end
end

function module.Ban(name, reason)
	if not table.find(BlacklistedPlayers, name) then
		table.insert(BlacklistedPlayers, name)
	end

	if module.GetPlayer(name) then
		local success, errorm = pcall(function()
			module.GetPlayer(name):Kick(reason)
		end)
		if success then
			print("Successfully Banned")
		elseif errorm then
			print("Failed to ban")
		end
	end
end

function module.Unban(name)
	if table.find(BlacklistedPlayers, name) then
		local success, errorm = pcall(function()
			local pos = table.find(BlacklistedPlayers, name)
			table.remove(BlacklistedPlayers, pos)
		end)
		print(table.find(BlacklistedPlayers, name))
		if success then
			print("succesfully unbanned")
		elseif errorm then
			print("failed to unban")
		end
	end
end

return module

Testing code:

local m = require(game.ServerScriptService.ModuleScript)
local p = m.GetPlayer("test")
print(">> Banning player test")
m.Ban("test")
print(">> Player test is joining")
m.PlayerAdded(p)
print(">> Unbanning player test")
m.Unban("test")
print(">> Player test joining, again")
m.PlayerAdded(p)

Output:

  11:25:35.141  >> Banning player test  -  Edit
  11:25:35.141  kicked player lol  -  Edit - ModuleScript:8
  11:25:35.141  Successfully Banned  -  Edit - ModuleScript:39
  11:25:35.142  >> Player test is joining  -  Edit
  11:25:35.142  kicked player lol  -  Edit - ModuleScript:8
  11:25:35.142  >> Unbanning player test  -  Edit
  11:25:35.143  nil  -  Edit - ModuleScript:52
  11:25:35.143  succesfully unbanned  -  Edit - ModuleScript:54
  11:25:35.143  >> Player test joining, again  -  Edit
  11:25:35.144  not kicking  -  Edit - ModuleScript:24