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)
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
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