Ban System unbans to early

Right, so I’m making a ban panel. If I ban someone it works once, then for some reason it unbans you even though the remote event for the unban has not been fired,
please help!

Local Script That Fires Ban

Here
local button = script.Parent

button.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Ban:FireServer(script.Parent.Parent.Parent.upper.Target.Text, script.Parent.Parent.Parent.upper.Reason.Text)
	script.Parent.Parent.Parent.upper.TextLabel.Text = "Player banned."
	script.Parent.Parent.Parent.upper.TextLabel.TextTransparency = 0
	wait(3)
	script.Parent.Parent.Parent.upper.TextLabel.Text = "Teleport failed, please try entering the username again."
	script.Parent.Parent.Parent.upper.TextLabel.TextTransparency = 1
end)

Local Script That Fires UnBan

Here
local button = script.Parent

button.MouseButton1Click:Connect(function()

game.ReplicatedStorage.UnBan:FireServer(script.Parent.Parent.Parent.upper.Target.Text, script.Parent.Parent.Parent.upper.Reason.Text)

end)

Main Script

Here
local configModule = require(script.Admin.panel.Config)
local data = game:GetService("DataStoreService"):GetDataStore(configModule.DataStoreKey)
local configModule = require(script.Admin.panel.Config)
local admins = configModule.adminIDs

local BanMsg = "You have been permenantly banned from this game."

game.Players.PlayerAdded:Connect(function(player)
	for _, adminId in pairs(admins) do
		if player.UserId == adminId then
			local cloneUI = script.Admin:Clone()
			cloneUI.Parent = player.PlayerGui
			break
		end
	end
	local prevData = data:GetAsync(player.UserId)
	if prevData == nil then
		print("Player does not have to be banned!")
		prevData:SetAsync(player.UserId, false)
	elseif prevData == true then
		print("Player is banned.")
		player:Kick(BanMsg)
	end
end)

game.ReplicatedStorage.Ban.OnServerEvent:Connect(function(player, victim, reason)
	local found = game.Players:FindFirstChild(victim)
	
	if found then
		data:SetAsync(player.UserId, true) -- that inserts the player ban to DataStore
		found:Kick(data..' '..reason)
	end
end)

game.ReplicatedStorage.UnBan.OnServerEvent:Connect(function(player, victim, reason)
	data:RemoveAsync(player.UserId) -- that inserts the player ban to DataStore
end)

Note the whitelist is built into the ban script, any help would be appreciated :heart:

1 Like

Try to set the victim instead of the player in the OnServerEvent

local configModule = require(script.Admin.panel.Config)
local data = game:GetService("DataStoreService"):GetDataStore(configModule.DataStoreKey)
local configModule = require(script.Admin.panel.Config)
local admins = configModule.adminIDs

local BanMsg = "You have been permenantly banned from this game."

game.Players.PlayerAdded:Connect(function(player)
	for _, adminId in pairs(admins) do
		if player.UserId == adminId then
			local cloneUI = script.Admin:Clone()
			cloneUI.Parent = player.PlayerGui
			break
		end
	end
	local prevData = data:GetAsync(player.UserId)
	if prevData == nil then
		print("Player does not have to be banned!")
		prevData:SetAsync(player.UserId, false)
	elseif prevData == true then
		print("Player is banned.")
		player:Kick(BanMsg)
	end
end)

game.ReplicatedStorage.Ban.OnServerEvent:Connect(function(player, victim, reason)
	local found = game.Players:FindFirstChild(victim)
	
	if found then
		data:SetAsync(victim.UserId, true) -- that inserts the player ban to DataStore
		victim:Kick(BanMsg .. "\nReason: " .. tostring(reason))
	end
end)

game.ReplicatedStorage.UnBan.OnServerEvent:Connect(function(player, victim, reason)
	local victimUserId = game:GetService("Players"):GetUserIdFromNameAsync(victim)
	data:RemoveAsync(victimUserId) -- that inserts the player ban to DataStore
end)

Edit: Don’t know why you are banned now maybe you banned yourself before?

Oddly the script banned me??
My testers are not banned though.

Did you put your username in the TextBox or your testers name?

I pasted your script into the menu saved the game and It would not even let me in.

So I don’t know what happened.

So, I don’t really know the problem, but I will give you some information on what I did for my admin system, which may not be perfect but it works.

I like your code, but from my experience with BaseAdmin, this is what I coded for unbanning, if it detects the rank is ‘-1’

if rank==-1 then
				local banData
				pcall(function()banData = var.data2:GetAsync("_Ban"..plr.UserId)end)
				if not banData or not banData.time then
					local msg = ""
					local canChat = game.Chat:CanUserChatAsync(plr.UserId)
					if canChat then
						plr.Chatted:Connect(function(m)msg = m end)
					else
						local gui = script.GUI.BaseGui:Clone()
						local yesNo = script.GUI.Addons.YesNo:Clone()
						yesNo.Parent = gui.Frame.Window
						local click = script.GUI.Addons.ClickEvent:Clone()
						click.Parent = gui.Frame.Window
						click.Clicked.OnServerEvent:Connect(function(_, m)
							msg=m
						end)
						local desc = script.GUI.Addons.DescText:Clone()
						desc.Text = "PLEASE READ BEFORE ENTERING IN AN OPTION!\nYou may close this window after you have entered in an option. If you've already selected an option, you may change it by clicking another option."
						desc.Parent = gui.Frame.Window

						gui.Frame.Position = UDim2.fromScale(0, 1)
						gui.Frame.AnchorPoint = Vector2.new(0, 1)
						gui.DisplayOrder=9999
						gui.ResetOnSpawn = false
						gui.Frame.TitleBar.Text = "Would you like BaseAdmin to fix this problem?"
						gui.Parent = plr.PlayerGui
					end
					delay(5, function()
						plr.CharacterAdded:Connect(function()
							plr.Character:Destroy()
						end)
						plr:LoadCharacter()
					end)
					local mgui = notifyplayersystemmessage("BaseAdmin: join error 3.\n\n❌\n\nMore information:\nBaseAdmin cannot find your BanData! You can still play this game, but your rank will be reset.\n\nIf you wish to stay banned and rather contact an administrator, chat n\n\nIf you want to have your Admin rank reset, chat y.\n\nNote:\nIf you have chosen to reset your rank, you will be kicked and asked to rejoin to apply the changes.\nIf you have a VIP rank, group rank, gamepass rank, setup rank, or rank that was not set by admin, you may automatically obtain it after rejoining.\n\nChat within 45 seconds or you will be kicked! After chatting you must wait up to 45 seconds.\n\nWhy we can't get your rank back?\nBanData contains your previous rank. Your rank was set to -1 (banned) when you were banned, and BanData contains documentation on what your previous rank used to be.\nWe cannot find BanData, and your current rank is -1.\n\nIf you don't have chat, please use the yes/no options in the dialog box.", plr.Name, 60, 'e')
					local timeleft = 45
					while timeleft>0 do
						timeleft-=wait(1)
					end
					notifyplayersystemmessage("BaseAdmin is processing your request. BaseAdmin will tell you the result of your request in 10 seconds.", plr.Name, 90, 'e')
					wait(10)
					local gui = notifyplayersystemmessage("BaseAdmin BanSolver\n\nIf there is no foreground window, report this problem to SubtotalAnt8185.", plr, 999) do
						gui.BackgroundTransparency = 0
					end
					if msg == 'y' or msg=='yes' then
						local s, e = nil
						if var.data2 then
							s, e = pcall(var.data2.RemoveAsync, var.data2, plr.UserId)
						end
						if not e then
							e = "DEBUG and/or DataStore disabled."
						end
						if not s then
							msg = "BaseAdmin Ranking Error.\n❌\nWe're sorry for the inconvenience, but there's an error involving your ban rank.\nBaseAdmin couldn't rank you to 0 (non admin), so you will continue to be ranked -1 (banned).\nThis will result in you not becoming unbanned.\nThis error is rare, and to potentially fix it please contact support in the group wall (search up SubtotalAnt8185 in groups).\nThis inconvenience may be caused by an interference from another datastore system, BaseAdmin's loader being outdated, or a Roblox service outage.\nPlease respond soon as this fault doesn't happen often, and we want feedback from it. If you can, send this information:\n"..e.."\n\nIf the error message is related to DEBUG, please contact the developer."
						else
							msg = "Successful! BaseAdmin has unranked your banned rank! Please rejoin to play the game like a non-admin."
						end
						local gui = script.GUI.BaseGui:Clone() do
							gui.DisplayOrder = 500
							local desc = script.GUI.Addons.DescText:Clone()
							desc.Text = "Would you like to view the output in a SystemMessage? Selecting 'No' will exit the game. Selecting 'Yes' will display a larger message, ideal for mobile users. If you select 'Yes', the message will close automatically after 120 seconds.\n\nNote: After pressing 'yes', you may press 'no' to leave the game, this window will not close but you may minimize it."..((s==true and "\n\nWe hope you enjoyed BaseAdmin's BanResolver!") or "\n\nIf you encounter issues, it might just help to try again later.")
							desc.Parent = gui.Frame.Window
							local yesNo = script.GUI.Addons.YesNo:Clone()
							yesNo.Parent = gui.Frame.Window
							local clickEvent = script.GUI.Addons.ClickEvent:Clone()
							clickEvent.Parent = gui.Frame.Window

							gui.Frame.TitleBarButtons.close:Destroy()

							gui.Parent = plr.PlayerGui

							clickEvent.Clicked.OnServerEvent:Connect(function(_, option)
								if option == 'yes' then
									notifyplayersystemmessage(msg, plr, 9999, 'e')
									wait(120)
									plr:Kick(msg)
								else
									plr:Kick(msg)
								end
								gui:Destroy()
							end)
						end
						return
					end
					if msg ~= 'n' and msg ~= "no" then
						plr:Kick("BaseAdmin: join error 3. If you rejoin, BaseAdmin may give you a list of options to work around this error."..genericV.genericErrorMsg)
						return
					end
				end
				local msg = banData.message
				local time = banData.time
				local originalRank = banData.rank
				if time<tick() then
					plr.BaseAdminPlayerData.SetRank.Value = originalRank
					local s, e = pcall(var.data2.RemoveAsync, var.data2, "_Ban"..plr.UserId)
					local unbanMessage = banData.unbanMessage or 'Unbanned by BaseAdmin'
					local s2, e2 = pcall(function()
						local dd = var.data2:GetAsync("_Bans")
						local new = http:JSONDecode(dd)
						local i = tostring(plr.UserId)
						local v = new[i]
						v.previousBans[time]='Ban: '..v.status..' Original ban message: '..msg..' Unban message: '..unbanMessage
						v.message = 'PreviousBans for '..plr.Name
						v.status = "not banned"
						new[i] = v
						new = http:JSONEncode(new)
						var.data2:SetAsync("_Bans", new)
						repeat wait(2) until var.data2:GetAsync("_Bans")==new
					end)
					if not s2 then
						warn(e2)
						notifyplayersystemmessage("Warning:\nBaseAdmin has failed to clear your name from the BanLogs!\nThis means if someone looks in BanLogs, they may still see your name as if you were still banned!\nYou may be able to fix this issue by using the unban command.\nIf the error persists, inform SubtotalAnt8185 about this issue:\n\n"..e2.."\n\nNote: This error may be caused by Roblox issues. If you are in a roleplay game and users check banlogs, it may not be a bad idea to screenshot this and save it (Hint: press Print Screen once or press F12 twice. On Android phones, press the volume and power buttons at the same time to take a screenshot).\n\nThis message will close in 30 seconds.\n\nPossible Fixes:\n1. Rejoin\n2. Leave and try again later\n3. Ask a developer about this issue", plr.Name, 30, 'e')
						wait(30)
					end
					if not s then
						warn('cannot remove ban '..e)
						notifyplayersystemmessage("Unban Error 1\n❌\nBaseAdmin: Error removing ban! Contact an admin.\nIt appears the datastore is locked or currently unavailable.\nPlease try again later.\n\nIf you wish to contact a developer or SubtotalAnt8185 to fix this issue, send the data below:\n\n"..e, plr.Name, 10, 'e')
					else
						notifyplayersystemmessage("You have been unbanned.\n✅\nBaseAdmin requires you to rejoin to use commands. Chat 'no' to cancel.\nUnban message:\n"..unbanMessage, plr.Name, 10, true)
					end
					local saidNo
					saidNo = plr.Chatted:Connect(function(msg)
						if msg:lower()=='no' or msg:lower()=="'no'" or msg:lower()=="n" then
							saidNo:Disconnect()
							saidNo = nil
							notifyplayersystemmessage("The automatic rejoin operation was stopped.", plr.Name, 5, "push-notification2")
						end
					end)
					wait(10)
					if saidNo then
						saidNo:Disconnect()
						pcall(tele.Teleport, tele, game.PlaceId, plr)
						wait(5)
						notifyplayersystemmessage('Rejoin failed.\n❌\nPlease exit the game and manually rejoin.\nIf this is a possible issue, ask the developer to enable third party teleports.', plr.Name, 5, 'e')
						wait(5)
						plr:Destroy()
						return
					end
				end
				wait(1.5)
				local b = "You are banned. Contact an admin with an unbanning ability to get unbanned.\n\nThe ban message does not exist, this is a fallback message. The ban might not have been initiated correctly.\nIf this is an issue, you must contact a developer."
				if msg then
					b = msg
				end
				b = "BaseAdmin:\n"..b
				local bantext = {
					'❌',
					'⛔',
					'🚫',
					'⭕',
					'❎',
					'❓',
					'📛',
					'🛑',
					'❗',
					'🔴'
				}
				local hrs = (time-tick())/86400
				local bt = "The number of symbols will indicate the days you have left on your ban.\n"
				for i = 1, hrs/3 do
					bt..=bantext[math.random(1, #bantext)]
				end
				if config.SecureKick then
					plr:Kick(b.."\nHoursLeft:"..math.floor(hrs).."\nSecureKick is currently enabled.")
					return
				end
				bt..='\n<font size="7"><font color="rgb(60, 60, 60)">Hours left: '..math.floor(hrs)..'</font></font>'
				local txt, gui = notifyplayersystemmessage(bt, plr.Name, 100, 'e')
				gui.ResetOnSpawn = false
				local s = script.lg:Clone()
				s.Parent = gui
				plr.Character:Destroy()
				wait(20)
				plr:Kick(b)
				return
			end

You might be thinking how can I understand all of this!? which is why I’m here.

Basically, the simple thing it does is detects for this thing called BanData. BanData is documentation on the previous rank and the time the player should be unbanned.
Based on this, there’s only one thing that’s wrong with my code. The ban time can be different based on the server local time.

So, unlike doing this:

I did this:

t.unban = {function(Player, plr, ...)
	local msg = getFilteredMessage(Player, ...)
	local bmsg = ' unbanned by '..Player.Name..(msg or "")
	local userId = game.Players:GetUserIdFromNameAsync(plr)
	if userId then
		local banData = datastore:GetAsync("_Ban"..userId)
		if banData then
			datastore:SetAsync("_Ban"..userId, {
				message = banData.message,
				unbanMessage = bmsg,
				time = 0,
				rank = banData.rank
			})
			local new = {}
			for id, v in pairs(http:JSONDecode(datastore:GetAsync("_Bans"))) do
				if id==userId then
					v.status = "pending unban from "..Player.Name
					v.message..= bmsg
				end
				new[id]=v
			end
			datastore:SetAsync("_Bans", http:JSONEncode(new))

			notify("✅", Player.Name, 1, true)
		else
			notify("This player isn't banned!", Player.Name, 3, 'e')
		end
	end
end, 'Unbans the target player.', '<plr>', 5}

For unbanning.

Unfortunately, I didn’t have any luck with tables, so I used JSON instead.
The “_bans” datastore contains all bans. Don’t worry about that.
The “_Ban” datastore contains the BanData. That thing I was talking about earlier. What this does is sets the time to 0 so the user is no longer banned when they join again, since the script will look if the local time is greater or less than the ban time.

I know this is lots of information to digest, but I think this is a more detailed explanation of what BaseAdmin does. And it does work, so if you’re willing to try a method like this and it works, that’s good for you.

2 Likes

This is a great solution!

Edit: I think he’s just getting into scripting and wants to make a simple Admin Panel, your information is perfectly fine but for one whos just getting started its kinda complicated to understand

Yeah no totally agree with this. Very confusing for me to comprehend.

1 Like

Never truly responded to this, no sadly the unban still didn’t work.
Also though Sub made an amazing recommendation my brain cannot comprehend it.

Well since you are asking, I will try and simplify it for you.

So, first thing to learn is BanData. BanData contains info on what time you will be unbanned as well as your previous rank (so it can get your rank back and set it when you are unbanned). Here is an image on what BanData contains:


BanData contains my rank (6) and the time I will be unbanned. It also contains the ban message.

So, upon joining, BaseAdmin will check if your rank is banned (ranks are not stored in BanData). If it is banned, it goes into the datastore to see what time you are supposed to be unbanned. If the time you are supposed to be unbanned is less than the current tick(), then you will be unbanned.

If you are unbanned, BaseAdmin will retrieve your previous rank and set it to your rank, therefore it’s like nothing ever happened. Actually, it does log the ban, but that’s not on topic here.

Here’s how it bans players:
To put it simply, BaseAdmin will just set the BanData and then run the rank command to rank the user ‘-1’, and then kick the user. The rank command is very complex, so I won’t be explaining it.

Here’s how it unbans players (using the unban command):
The Unban command basically does what you do upon joining and your time is up, but it has the ability to set an unban message. It will take your current ban data and set the time to 0, therefore when joining it looks like your time is up.

I hope this made a little more sense. If you’re still stumped, here’s what the very, very ancient AdminSuite v1.21 did with server banning:

function ban(plr)
	local plrs = game.Players:GetChildren()
	--This is AdminSuite 1.01 developed in 2021 so please do not get mad at me for my very messy coding. Also, yes, the formatting actually looked like this.
	
	
	
	for i=1,#plrs do
		local match = string.match(string.lower(plrs[i].Name), plr, 1)
		if match then
			local isWrittenAdmin = false
			for i=1,#admins do
			if plr.Name==admins[i] then
			isWrittenAdmin=true
			end
			end
			if isWrittenAdmin==false then
			local name = plrs[i].Name
			plrs[i]:Kick("ServerBanned by an admin.")
			game.Players.PlayerAdded:Connect(function(plr)
				if plr.Name==name then plr:Kick("ServerBanned") end
			end)
			else
			CdisplayMessage("Someone attempted to use an abusive command on a setting declared admin (breaking rule 1 of 42)")
			end
		elseif plr=="all" then
			CdisplayHint("Someone attempted to use a oneTimeOnly command (kick/ban) on all (denied access) (breaking rule 2 of 42)")
		end
	end
end

Alright, I know you didn’t see real problem I mentioned in my previous post, so I will say it here: The problem in the BaseAdmin ban system is that the server is in a different timezone. Bans might be shifted a few hours depending on where you join. I’d love to see a youtuber do a video and say “im banned here but not here this game is broken” but at the same time I should probably fix that.

1 Like