There are many ways to do this, and it’s up to you to use whichever you want. This is a walkthrough on the datastore way, so let’s begin.
First, let’s create a script in ServerScriptService and get the DataStoreService from the game. (You do not have to read the comments I put in if you are experienced at scripting.)
local DSS = game:GetService("DataStoreService")
Alright, now, let’s get a datastore from DataStoreService called “Bans” (you may change this at your own will)
local bansDataStore = DSS:GetDataStore("Bans")
Now, let’s stop a bit here and think;
What do we want the game to do with those 2 variables?
We want the game to check if a player is banned or not, EVERY SINGLE TIME they join the game. We can use PlayerAdded to do that. So, so far we got:
local DSS = game:GetService("DataStoreService")
local bansDataStore = DSS:GetDataStore("Bans")
game.Players.PlayerAdded:Connect(function(player)
end)
Now you might be saying: “This is not checking if a player is banned or not?”
That’s where GetAsync()
comes in. Let’s add that to the script.
local DSS = game:GetService("DataStoreService")
local bansDataStore = DSS:GetDataStore("Bans")
game.Players.PlayerAdded:Connect(function(player)
bansDataStore:GetAsync("banstatus_"..player.UserId) -- You may change banstatus_ and player.UserId (limited) at your own will, but it is STRONGLY recommended to use UserIds in order to check for bans.
end)
But all we did here was to check if the value existed or not. That’s where we gotta use Kick()
.
Let’s use some logic first, if you don’t set any keys to a datastore, then the value is obviously going to be nil
. Let’s make it, so that if the value is NOT nil
, kick the player everytime they join.
And now we have:
local DSS = game:GetService("DataStoreService")
local bansDataStore = DSS:GetDataStore("Bans")
game.Players.PlayerAdded:Connect(function(player)
if bansDataStore:GetAsync("banstatus_"..player.UserId) then
player:Kick("put reason here") -- This'll be the kick reason for ALL PLAYERS that are banned, NOT individually.
end
end)
Wait! We aren’t done yet! We gotta use SetAsync()
on your admin panel to actually ban the player, this is the most important part yet!
Alright, let’s calm down a little bit. THIS is our current script INSIDE THE ADMIN PANEL, right?
local TextBox = script.Parent.Parent.TextBox
script.parent.MouseButton1Click:Connect(function()
if game.Players[TextBox.Text] then
game.Players[TextBox.Text]:Kick()
end
end)
Wow, it actually wasn’t gonna be that hard I guess… we just have to add the same variables to this script, add SetAsync()
, annnnd;
local DSS = game:GetService("DataStoreService")
local bansDataStore = DSS:GetDataStore("Bans")
local TextBox = script.Parent.Parent.TextBox
script.parent.MouseButton1Click:Connect(function()
if game.Players[TextBox.Text] then
local playertoban = game.Players[TextBox.Text] -- To specify the Player instance.
bansDataStore:SetAsync("banstatus_"..playertoban.UserId,1) -- Make sure the values inside this is the same within the ones inside the script we just made!
game.Players[TextBox.Text]:Kick()
end
end)
You can set the “1” to anything you want, because the game is not checking for a specific value, it only checks if the value is nil
, or not.
And now, a very important note here: If you want to UNBAN the player, you gotta make an unban script and set the value to nil
, you can also use RemoveAsync()
, which is a much better option.
If you have been following this step-by-step (without changing anything), then you must know the player’s UserId in order to unban them. If you’re gonna put an unban section inside the same script, use this:
bansDataStore:RemoveAsync("banstatus_"..playertounban.UserId) -- Remember to set the playertounban using a variable!
But if you’re gonna use a seperate script, or just unban the player using the command bar, use this:
local DSS = game:GetService("DataStoreService")
local bansDataStore = DSS:GetDataStore("Bans")
bansDataStore:RemoveAsync("banstatus_"..playertounban.UserId) -- WARNING: If you are using the command bar in order to unban the player, you will need to specify the UserId yourself without using commas! If you are using a seperate script, you will have to set playertounban using a variable!