Ban Moderation System

So i’m trying to make a Ban System that has a ban Menu for moderators, and a Ban System for a Anti-Cheat

So My data For the ban Doesn’t save and all like i made the Gui and it kicks teg player but it wouldn’t save them to the Datastore

a Solution i tried was probably go to Developer hub and Youtube

i’m right now in roblox studio figuring out how i can Do this Ban Menu.

2 Likes

You can’t ban players without using any sort of datastore. It just is not possible.

1 Like

I Know that, its just that i’m stuck on making it ban a player by putting their UserId in a Datastore

1 Like

Oh I see my apologies. Give me a moment.

local DDS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local BanStore = DDS:GetDataStore("BanStore")
local secondsInADay = 86400

local function SetBan(Player, Reason, Duration)
	local Success, Error = pcall(function() --Protect the call in case the player doesn't exist or the player wasn't found or anything that may cause an error that breaks the whole script;
		BanStore:SetAsync(tostring(Player.UserId), {BanStart = os.time(), BanDuration = (Duration * secondsInADay), BanReason = Reason});
	end)
	Player:Kick(Reason);
	if not Success then --for debugging
		warn("Not successful.")
	end
end

local function GetBan(Player)
	local Success, Result = pcall(function()
		return BanStore:GetAsync(tostring(Player.UserId), "TempBan");
	end)

	if Success then --See if DataStore request was successful or not in order to prevent running into any errors
		if Result then --see if ban data exists for the specified player or not
			if Result.BanStart + Result.BanDuration > os.time() then --see if ban duration has passed
				return nil;
			else
				return Result.BanReason;
			end
		end
	end
end

Players.PlayerAdded:Connect(function(plr)
	local IsBanned = GetBan(plr);

	if IsBanned ~= nil then
		plr:Kick(IsBanned);
	else
		print("Player's ban has been lifted.");
	end
	
	if plr.UserId == game.CreatorId then --restrict this command only to the owner
		plr.Chatted:Connect(function(message)
			if string.sub(message, 1, #"/ban"):lower() == "/ban" then
				local args = string.split(message, " "); --split string message via space, and iterate them into a table
				local numbers = "%d" or "%d%d"; -- string pattern to look for duration/numbers
				local duration = string.sub(message, string.find(message, numbers));
				local playerToBan = Players:FindFirstChild(args[2]); --search for player via specified playername in command
				local reason = string.sub(message, string.len("/ban " .. args[2] .. " " .. duration) + 1); --search for reason
				if playerToBan then
					if duration then
						if reason then
							SetBan(playerToBan, reason, duration);
							print("Player has been banned!");
						end
					end
				end
			end
		end)
	end
end)

This is all in a server script.

2 Likes

Wow, i just have one question this will work with Gui right
because i’m banning people from a gui

1 Like

i would suggest editing like 12 to being:

warn("Not successful. Error:"..Error)

so you can see what went wrong in the dev console

One Question, how would i make this Ban from a gui because this bans people from a Admin Command

Anotehr Question For the Function for GetBan when it says return BanStore:getAsync()
do i have to say tostring because you would be only saying that in AdminCommands

1 Like

Yes. You’d just need to use remote events to connect from the local ui to the server.

Okay, this is my code for my Ban Gui So far.

local DDS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local BanStore = DDS:GetDataStore("BanStore1")
local secondsInADay = 86400
local Admins = {
	"DeveloperSiverity",
	"Renzpalo1",
	"chickeip"
}

script.Parent.Remotes.Ban.OnServerEvent:Connect(function(plr, Player, Reason, Duration)
	for i,v in pairs(Admins) do
		if plr.Name == v then
			local success, Error = pcall(function()
				
				BanStore:SetAsync(game.Players:GetUserIdFromNameAsync(Player), {BanStart = os.time(), BanDuration = (Duration * secondsInADay), banReason = Reason})	
				print("Entered into Ban Datastore")
			end)
			game.Players:FindFirstChild(Player):Kick("You have been banned from this game By a admin For "..Reason)
			if not success then
				warn("Failed")
			end
		
			
		end
		
		
	end
		
	
	
	
end)

I put this in a script in the gui called main handler so this is in the ScreenGui

Edit: I made a script in serverscriptservice
and i made a Warn that will tell me if i’m entered into the Datastore but i’m not entered into the datastore at all