Hi, I’m using DataStore in my game to suspend someone if he’s cheated:
local DataStore = game:GetService("DataStoreService"):GetDataStore(Player.UserId.."Data")
local Data = DataStore:GetAsync("Suspended")
But how can I make it to automatically unsuspend the player if player was suspended for 100 days? (I don’t know how to use os.time() in scripting). Please, help me!
os.time() returns the time in seconds since the start of the year.
If you save os.time(), when the player joins, you can subtract it from the current os.time() - and if the result is greater than the number of seconds 100 days would take, don’t kick them. Otherwise, kick them.
by the way, also use a pcall() with GetAsync().
local timeGap = os.time() - Data
if timeGap > The_100_days_in_seconds_here then
--player doesn't get kicked
end
--example
local saveData = {
["Banned"] = true,
["SuspensionTime"] = os.time()
}
--Do not save this data when a player leaves.
--It would mess it up if it saved every time a banned player joined and got kicked.
local success, result = pcall(function()
DataStore:SetAsync(key, saveData)
end)
if not success then
warn(result)
end
Then, on loading:
--example
local timeSeconds = 8640000
local data
local success, result = pcall(function()
data = DataStore:GetAsync(key)
end)
if success and data then
if os.time() - data.SuspensionTime <= timeSeconds then
player:Kick("You are still banned.")
end
end
having a value is already not false, therefore, setting the value of “Banned” as the time they were banned (or even better, the time when the Ban will be done) already counts as “true”.
just check make the “is banned check” a " tonumber( value )~=nil " and you’re good.
Is banned? value is a number.
isn’t banned? value is nil.
no booleans needed, information can be stored there.