Help script not working correctly os.time()

Hello, I am making a temporary ban system when a wrong door is touched but I have a issue with the os.time() it does not work correctly in this script and I don’t know why. If anyone knows what is the reason, please tell me thanks!

Script:

local BanPart = game.Workspace.banPart
local memoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = memoryStoreService:GetSortedMap("BansData")
local banTime = 3600

local Days = "%d"
local Hours = "%H"
local Minutes = "%M"
local now = os.time()

BanPart.Touched:Connect(function(touchPart)
	local character = touchPart.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player == nil then return end
	local success, value = pcall(sortedMap.SetAsync, sortedMap, player.UserId, os.time() + banTime, banTime)
	player:Kick("You are currently banned.\n Reason: You touched the wrong door. \n Time Left to play: " .. os.date(Days, banTime) .. " Days, " .. os.date(Hours, banTime) .. " Hours, " .. os.date(Minutes, banTime) .. " Minutes, ")
end)

game.Players.PlayerAdded:Connect(function(player)
	local success, value = pcall(sortedMap.GetAsync, sortedMap, player.UserId)
	if success == false then return end
	if value == nil then return end
	local Time2 = value - os.time()
	player:Kick("You are currently banned.\n Reason: You touched the wrong door. \n Time Left to play: " .. os.date(Days, Time2) .. " Days, " .. os.date(Hours, Time2) .. " Hours, " .. os.date(Minutes, Time2) .. " Minutes, ")
end)

image

1 Like

if we look here os
and look at os.date()

you can see that date will tell you about the date you pass into it for instance the day of the month it will not tell you how many days are remaining

so for instance if i do

print(os.date("%d", os.time())) -- this will print 26 because today is 26 / may / 2022

here is a way to convert seconds to text with days hours minutes and seconds

local memoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = memoryStoreService:GetSortedMap("ban")
local banTime = 15000

local function SecondsToString(seconds)
    local days = math.floor(seconds / 86400)
    local hours = math.floor(seconds % 86400 / 3600)
    local minutes = math.floor(seconds % 3600 / 60)
    seconds = math.floor(seconds % 60)
    return string.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds)
end

local banTimeText = SecondsToString(banTime)

workspace.BanPart.Touched:Connect(function(touchPart)
	local character = touchPart.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player == nil then return end
	local success, value = pcall(sortedMap.SetAsync, sortedMap, player.UserId, os.time() + banTime, banTime)
	player:Kick("\n You are banned try to join in: \n" .. banTimeText)
end)

game.Players.PlayerAdded:Connect(function(player)
	local success, value = pcall(sortedMap.GetAsync, sortedMap, player.UserId)
	if success == false then return end
	if value == nil then then return end
	local deltaTime = value - os.time()
	local timeText = SecondsToString(deltaTime)
	player:Kick("\n You are banned try to join in: \n" .. timeText)
end)

you can change

string.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds)

to be anything you like for instance

string.format("%d Days, %02d Hours, %02d Minutes, %02d Seconds", days, hours, minutes, seconds)
3 Likes

What is the issue exactly, it looks fine looking at the image

Thanks for your reply I did small modifications to the script and it is working correctly.
This is the script:

local BanPart = game.Workspace.banPart
local memoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = memoryStoreService:GetSortedMap("ban9")
local banTime = 3600

local function SecondsToString(seconds)
	local days = math.floor(seconds / 86400)
	local hours = math.floor(seconds % 86400 / 3600)
	local minutes = math.floor(seconds % 3600 / 60)
	seconds = math.floor(seconds % 60)
	return string.format("%d Days, %02d Hours, %02d Minutes", days, hours, minutes)
end

local banTimeText = SecondsToString(banTime)

BanPart.Touched:Connect(function(touchPart)
	local character = touchPart.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player == nil then return end
	local success, value = pcall(sortedMap.SetAsync, sortedMap, player.UserId, os.time() + banTime, banTime)
	player:Kick("\n You are banned try to join in: \n" .. banTimeText)
end)

game.Players.PlayerAdded:Connect(function(player)
	local success, value = pcall(sortedMap.GetAsync, sortedMap, player.UserId)
	if success == false then return end
	if value == nil then return end
	local deltaTime = value - os.time()
	local timeText = SecondsToString(deltaTime)
	player:Kick("\n You are banned try to join in: \n" .. timeText)
end)
1 Like