Hello, I’m making a ban system script for my game, using DataStoreService.
Ban saving works, but I’m not sure unban and time ban is working correctly, and I’m not that good at DataStores, so the script may not work properly, thanks for your response!
Server side script:
--// Services
local DSS = game:GetService("DataStoreService")
local RS = game:GetService("ReplicatedStorage")
--// Variables
local BanDataStore = DSS:GetDataStore("BanDataStore_3")
local banEvent = RS:WaitForChild("BanEvent")
local kickEvent = RS:WaitForChild("KickEvent")
local secondsInOneDay = 86400
--// Security Code
local Code = "2SLD25637ZNAE7LL9XC8PNFVC473EEBZ4G975GU63D7CXLTJR23CXRKLXXT42745RERMB2ARCSQSTQR3YMAWXE7HS42J9L43FFJ4TEPESNXBLU5BCJXVL84QUY2ZKBSMQJHY5MWCPUF27VY792WD3N6Y7TENTTH7TWGQYCXSPP9NTNN463KHSJNALUEUG366QFCEGSTQU878DJ64SP2KLBL2H53ZJJFUCJYSM8TNQYQV3ECPJ44Z4MBCEQMM9LC8T6E4QHF63WX75R46M4HT6D3AJ99EUHPN2UKVVBXYRBTRQGLZM6Z6KMH3L33"
--// Functions
local function banPlayer(player, reason, lenght)
if game.Players:FindFirstChild(player.Name) then
local success, errmsg = pcall(function()
BanDataStore:SetAsync(player.UserId.."-ban", true)
BanDataStore:SetAsync(player.UserId.."-reason", "You are banned for: "..lenght.." (seconds, calculated by os.time! Moderator notes: "..reason.." Wish to appeal? Join our discord server!")
BanDataStore:SetAsync(player.UserId.."-lenght", lenght)
BanDataStore:SetAsync(player.UserId.."-LastTimeWasOn", os.time())
end)
if success then
print("Successfully banned "..player.Name.."!")
else
print("Oops, looks like something went wrong, and we can't ban"..player.Name.."! Please try again!")
end
player:Kick("You are banned for: "..lenght.."! Moderator notes: "..reason.." Wish to appeal? Join our discord server!")
end
end
--// Main
game.Players.PlayerAdded:Connect(function(plr)
local lenght
local banned
local reason
local wasOn
local success, errmsg = pcall(function()
banned = BanDataStore:GetAsync(plr.UserId.."-ban")
reason = BanDataStore:GetAsync(plr.UserId.."-reason")
lenght = BanDataStore:GetAsync(plr.UserId.."-lenght")
wasOn = BanDataStore:GetAsync(plr.UserId.."-LastTimeWasOn")
end)
if lenght ~= nil and wasOn ~= nil then
if wasOn >= lenght then
BanDataStore:RemoveAsync(plr.UserId.."-ban")
BanDataStore:RemoveAsync(plr.UserId.."-reason")
BanDataStore:RemoveAsync(plr.UserId.."-lenght")
BanDataStore:RemoveAsync(plr.UserId.."-LastTimeWasOn")
end
end
if success then
print("Successfully loaded ban data!")
if banned then
if banned == true then
plr:Kick(reason)
end
end
else
print("Something went wrong with datastore, ban data loading failed!")
end
end)
--// Events
banEvent.OnServerEvent:Connect(function(sender, plrToBan, reason, lenght, code)
if code == Code then
banPlayer(plrToBan, reason, lenght)
end
end)
And the local script in StarterGui:
local Code = "2SLD25637ZNAE7LL9XC8PNFVC473EEBZ4G975GU63D7CXLTJR23CXRKLXXT42745RERMB2ARCSQSTQR3YMAWXE7HS42J9L43FFJ4TEPESNXBLU5BCJXVL84QUY2ZKBSMQJHY5MWCPUF27VY792WD3N6Y7TENTTH7TWGQYCXSPP9NTNN463KHSJNALUEUG366QFCEGSTQU878DJ64SP2KLBL2H53ZJJFUCJYSM8TNQYQV3ECPJ44Z4MBCEQMM9LC8T6E4QHF63WX75R46M4HT6D3AJ99EUHPN2UKVVBXYRBTRQGLZM6Z6KMH3L33"
local part = game.Workspace.BanPart
local secondsInOneDay = 86400
local db = false
local days = 0.005
part.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) and db == false then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
game.ReplicatedStorage.BanEvent:FireServer(plr, "You are banned LOL!", os.time() + days * secondsInOneDay, Code)
db = true
wait(5)
db = false
end
end)
I’m planning to make ban GUI, but now I’m just testing it with part.
Note: I’m pretty new to scripting, so I found os.time()
only today, and idk how to use it, but I tried to do something…