How can I make a system to ban and unban someone?

There are probably things for this but I want to know how you do it, that way you dont just look up a tutorial and guess if it works, I need to make sure its not outdated ya know ya know.

Any ways thanks!

4 Likes

Datastores are pretty much the only way. Whenever you want to ban someone, just give them a special tag in a store, then kick them.

You can check for that tag on join and just kick them whenever they try to join.
Here’s an example from my own admin tools script that I made a bit ago:

function CheckForBan(Player: Player)
	local CurrentTime = os.time()
	local BanInfo = NovaAdminBans:GetAsync(Player.UserId)
	
	if BanInfo then
		if AdminList:RetrieveAdminLevel(Player.UserId) >= 3 then
			print("Banned admin; backpedaling.")
			NovaAdminBans:SetAsync(Player.UserId, {["BanUntil"] = "null", ["BanType"] = "null"})
		end
		
		if BanInfo.BanUntil > CurrentTime and BanInfo.BanType == "Temp" then
			local DateTable = GetReadableTimeFromUnix(BanInfo.BanUntil - CurrentTime)
			Player:Kick("You have been temporarily banned. Time until Unban: ["..DateTable.Days.." days, "..DateTable.Hours.." hours, "..DateTable.Minutes.." minutes, "..DateTable.Seconds.." seconds.]")
		end
		
		if BanInfo.BanType == "Perm" then
			Player:Kick("You have been permanently banned.")
		end
	end
	
end

PlayerService.PlayerAdded:Connect(CheckForBan)
2 Likes

What is this? Is this a custom function you made? Cause its not a thing for me.

Also thanks! Almost finished writing this whole thing out and testing it! Thanks again!

4 Likes

Now I know how to save the datastore and ban a user(Thanks btw) but how do I get the time to ban them for? Lets say I wanted to ban them for a day, how would I take the current time and add 1 day?
Also thanks!

1 Like

You’d save the time (os.time) they were banned in the datastore and when they join (Players.PlayerAdded), you just subtract the saved value by os.time.

Let’s say you banned them and os.time is 6 hours (ITS A SMALL NUMBER TO MAKE IT EASY).

You wanna ban them for 6 more hours. When they rejoin 4 hours later (os.time = 10 hours), you subtract os.time by the saved value and find out that they have to wait 2 more hours.

And finally they wait 2 hours and you check if the saved value subtracted by os.time is = 0 (it’s been 6 hours) or is less than 0 (which means it’s been more than 6 hours).

This entire post is explaining this basically

1 Like

Just wondering, but if I banned them for a whole week, if I did os.time wouldnt that return just the time and not the date right? cause if they join the next day at the exact time they got banned, os.time would be the same therefore they would be unbanned, unless os.time includes the date they got banned and the time

2 Likes
function GetReadableTimeFromUnix(UnixTime: number)
	local DateTable = {
		Days = math.floor(UnixTime/86400),
		Hours = math.floor(UnixTime/3600) % 24,
		Minutes = math.floor(UnixTime/60) % 60,
		Seconds = UnixTime % 60
	}
	return DateTable
end

This is just so that you can read how much time you have left until you are unbanned.

Easy. I just use os.time()
It gives you the current unix time, which is just a count of seconds since Jan 1st 1970.

Since we are dealing with seconds, if you want to ban someone for a day you would just do:
60 Minutes * 60 Minutes * 24 Hours (86,400)

Then I add that to the os.time() from earlier and save it in the datastore. (So what we basically did was just (os.time() + 86400).
Just subtract the time it is for the person when they join from the server.
os.time() - Time we calculated earlier
And if its 0 or less, then they can safely join.
OR you could just do
if os.time() >= TimeUntilUnban then Unban() end

2 Likes

Whoops. I guess simplifying it made it confusing.

os.time returns the time since the Unix Epoch (January 1, 1970 00:00:00 Universal TS). So basically it doesn’t reset every new day.

1 Like

oh ok this makes much more senes! Thank you!

1 Like

Just a quick question, what do I set the BanUntil value to? A number or a time? From the look of things I am going to assume a number but I wanted to make sure yk yk

It’s going to be the unix time that they get unbanned on. So, os.time() + SecondsUntilUnban

2 Likes

well roblox has a ban API now so ima just use that lol

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.