Greetings! I’m interested on making a time-ban script but I’m not too sure how I would go around doing it. I tried using Datastores but they didn’t save as needed. I’m a little lost here, as to what I should do since there aren’t any tutorials explaining this. Help is appreciated!
I would do it something like this:
- Admin runs the ban function
- Your system kicks the player if in game, and then save their key along a time you want to ban them for, for this purpose you should look into os.time() function
Whenever you ban someone, you’d need a datastore to keep track of it. Then, if their key in the datastore has data in it, you know they’re banned. To make a time ban, whenever you ban someone, save the os.time()
and the length you want them banned for (in seconds. Most mod menus including mine let you input in days, though, and this is done by multiplying the input by 86,400, which is how many seconds is in a day). If you want a permanent ban, save the length as math.huge.
Then, whenever a player joins the game, if they have a key in the ban datastore, which indicates that they’re banned, subtract the os.time()
you saved in the datastore from os.time()
, and if it’s longer than the length you saved in the datastore, the time ban is over, and clear their key in the datastore using :RemoveAsync()
. If it’s less than the length, kick the player because the time ban isn’t over yet.
To make the length, do I subtract the player data I stored via datastore with seconds or os.time() ?
If you wanted to ban someone for a day, save the length as 86400 seconds, and the os.time(), which means the time they were banned. Then when I say when the player joins back, subtract the os.time() in the datastore from os.time(), that means subtract the time they were banned from the current time, which as a result gives you how much time has passed since they were banned, in seconds. If it’s greater than the 86400 you saved, which is the length, the time for the time ban is up.
I’ve done this, but I’m recieving an error:
Here’s my code:
local secondsInADay = 86400
local lastOn = os.time()
-----------------
--- FUNCTIONS ---
-----------------
local DDS = game:GetService("DataStoreService");
local Players = game:GetService("Players")
--// DECLARABLES/VARIABLES
local BanStore = DDS:GetDataStore("BanStore") --scope is optional
----------------------
--- INITIALIZATION ---
----------------------
Players.PlayerAdded:Connect(function(plr)
game.ReplicatedStorage.consoleRemotes.BanTimes.oneDay.OnServerEvent:Connect(function()
local bansData = BanStore:GetAsync(plr.UserId.."-Bans")
if (bansData - lastOn) >= secondsInADay then
bansData:RemoveAsync(plr.UserId.."-Bans")
end
if bansData ~= nil then
plr:Kick("You are banned! You have 1 day left before your ban is finished.")
else
bansData:SetAsync(plr.UserId.."-Bans", os.time())
end
end)
end)
The problem is that your data might be nil so your subtracting nil, try this:
local secondsInADay = 86400
local lastOn = os.time()
-----------------
--- FUNCTIONS ---
-----------------
local DDS = game:GetService("DataStoreService");
local Players = game:GetService("Players")
--// DECLARABLES/VARIABLES
local BanStore = DDS:GetDataStore("BanStore") --scope is optional
----------------------
--- INITIALIZATION ---
----------------------
Players.PlayerAdded:Connect(function(plr)
game.ReplicatedStorage.consoleRemotes.BanTimes.oneDay.OnServerEvent:Connect(function()
local bansData = BanStore:GetAsync(plr.UserId.."-Bans")
if bansData ~= nil then
if typeof(bansData) == 'number' then
if (bansData - lastOn) >= secondsInADay then
bansData:RemoveAsync(plr.UserId.."-Bans")
end
else
warn('NaN')
end
end
if bansData ~= nil then
plr:Kick("You are banned! You have 1 day left before your ban is finished.")
else
bansData:SetAsync(plr.UserId.."-Bans", os.time())
end
end)
end)
Yeah, os.time() was subtracting nil, I tried this but it’s still nil.
You are doing:
bansData:SetAsync(plr.UserId.."-Bans", os.time())
But this should be
banStore:SetAsync(plr.UserId.."-Bans", os.time())