Temp-Ban UnBanning

Hello!
I’ve made a temp-ban script by using _G, but I got a problem with unbanning.
When I try to unban someone, nothing happens

Ban Function:

Ban
function Ban(plr, reason, bantime)
	local UserId = game.Players:GetUserIdFromNameAsync(plr)
	local PlayerName = game.Players:GetNameFromUserIdAsync(UserId)
	local Success, Error = pcall(function()
		if bantime == "Day" then
			tempData:SetAsync(tostring(UserId), {BanStartTime = os.time(), bantime = 86400}, reason)
			Event:FireAllClients(PlayerName, "Ban", "DAY-BANNED")
		elseif bantime == "3Day" then
			tempData:SetAsync(tostring(UserId), {BanStartTime = os.time(), bantime = 259200}, reason)
			Event:FireAllClients(PlayerName, "Ban", "3-DAY-BANNED")
		elseif bantime == "Week" then
			tempData:SetAsync(tostring(UserId), {BanStartTime = os.time(), bantime = 604800}, reason)
			Event:FireAllClients(PlayerName, "Ban", "WEEK-BANNED")
		elseif bantime == "Perm" then
			tempData:SetAsync(tostring(UserId), {BanStartTime = os.time(), bantime = 9999999999999999999999999}, reason)
			Event:FireAllClients(PlayerName, "Ban", "PERM-BANNED")
		end
		wait(2)
		if game.Players:FindFirstChild(PlayerName) then
			local toBan = game.Players:FindFirstChild(PlayerName)
			toBan:Kick(reason)
		end
	end)
	if not Success then
		print(Error)
	end
end

Unban Function – which doesn’t work

Unban
function UnBan(userId)
  local data = game:GetService("DataStoreService"):GetDataStore("BanData")
  data:RemoveAsync(userId, reason, bantime)
end

I am really bad at dataStores

3 Likes

Have you tried to Instead of using remove async do a SetAsync with os.time() -1 as bantime?

he’s right you should have done

tempData:SetAsync(tostring(UserId), {BanStartTime = os.time(), bantime = 9999999999999999999999999, reason = reason}) 

:SetAsync(Key,Data) Data is for you a table. so it woudent store the Reason. just the banstart time and bantime and i dont think you need to store the ban start time. just Bantime = os.time() + bantimeinseconds

2 Likes

Looking through your code, it doesn’t seem like you are using the DataStore functions correctly. Is there any output related to this script that could help find the problem?

SetAsync() only accepts two parameters, that is the store key and the value.
RemoveAsync() only accepts one parameter, that is the store key.

For consistency, you should also consider using tostring on all of the places you are using a userId for the DataStore key.

What does your code look like to checking if someone is banned as well?

Didn’t tried yet, let me try rn

Pretty sure you have to remove and add them 1 at a time. Also if you’re confused of how to remove the reason, and bantime start from the beginning where you banned the player and add 2 other values inside of the playerkey which is the player’s id. I don’t know if this would work, but you could give it a try.

_G.UnBan = function(userId)
  local data = game:GetService("DataStoreService"):GetDataStore("BanData")
  data:RemoveAsync(userId[reason])
  data:RemoveAsync(userId[bantime])
  data:RemoveAsync(userId)
end
1 Like

This one didn’t worked, i think i need to try updating async

Okay, there

game.Players.PlayerAdded:Connect(function(plr)
	if plr.UserId == game.CreatorId then return end
	local Success, Result = pcall(function() -- It is good practice to wrap your datastore requests in pcalls
		return tempData:GetAsync(tostring(plr.UserId), "TempBan", reason) -- Returns the players temp ban data for later use.
	end)
	
	if Success then -- Checks if the DataStore request was successful 
		if Result then -- Checks if there is any data under the key
			if Result.BanStartTime + Result.bantime > os.time() then -- This is the part that checks if the player is temp banned or not
				local status = tempData:GetAsync(tostring(plr.UserId), "TempBan", reason)
				
				local t = os.date('!*t',Result.BanStartTime + Result.bantime)
				if Result.bantime > 999999 then
					plr:Kick("You are banned until: ETERNITY")
					return
				end 
				local month = t.month
				local day = t.day
				local year = t.year
				
				if status then
					plr:Kick("You are banned until: "..month.."/"..day.."/"..year)
				end
			else
				print('Thing')
			end
		end
	end
end)

I’d try simply using SetAsync instead of RemoveAsync. There’s not much of a point to using RemoveAsync when SetAsync allows for much more flexibility in what replaces it.
Also, I wouldn’t use _G declarations for ban/unban methods.

1 Like

Thanks, this one helped me!! :smile:

1 Like