Wait for a specified amount of time in real life

Alright, here I have:

Count time function:

function CountTime(plr, Time)
	print("start count")
	local jail = true
	local timeToFree = tick() + Time -- jail time in seconds
	local saveTime = timeToFree - tick()

	game.Players.PlayerRemoving:Connect(function(plrRemoved)
		saveTime(plrRemoved,saveTime) -- error is here
		print("saved time")
	end)

	while jail do
		print(timeToFree)
		wait(1)
		if tick() > timeToFree then
			print("unjailed")
			JailModule.Unjail(plr)
			jail = false
		end
	end
end

When it goes to the saveTime() in the Count Time function, it gives me an error:
image

you have saveTime value and saveTime() function exactly the same time in a code

try changing the name.

1 Like

plus saveTime value is pointless, because timeToFree - tick() = Time

1 Like

Also, now I have a problem with DataStore. It saves the time normal as far as I see, but the DataStore doesn’t detect the player when he joins the game.
That is how I save the Time and UserId in DataStore:

	local info = {plr.UserId; Time}
	JailDS:SetAsync(info, true)

And when the player joins:

	local jailed
	local success, errormsg = pcall(function()
		jailed = JailDS:GetAsync(plrUserId)
	end)

But the problem is, it just doesn’t detect anything, doesn’t write anything in the output. I guess its the problem in the DataStore, in the

local info = {plr.UserId; Time}

But I don’t know how to fix it, nor how to write it the other way.

You’re trying to set the DataStore key as the player data:

local UserId = tostring(Player.UserId)

local success, result = pcall(function()
    return JailDS:GetAsync(UserId)
end)

print(result)

-- to overwrite the jail data
JailDS:SetAsync(UserId, Time)
1 Like

Alright, I still have a problem.
I get this error:

Line 87:

function CountTime(plr, Time)
	print("start count")
	local jail = true
	local timeToFree = tick() + Time -- ERROR HERE LINE 87

Line 77:

if jailed then
			for _,part in pairs(JailSpawns:GetChildren()) do
				table.insert(JailSpawnsTBL, part.CFrame)
			end

			wait(1)
			
			CountTime(plr, TimeToJail) -- ERROR HERE LINE 77
			plr.Character:SetPrimaryPartCFrame(JailSpawnsTBL[math.random(#JailSpawnsTBL)])
			char.Jailed.Value = true
		end

TimeToJail is a variable, that is nil by default:

local TimeToJail

But in a function, it makes it equal to Time variable.

function JailModule.Jail(plr, Time, reason)
	local UserId = tostring(plr.UserId)
	local info = {UserId; Time}
	TimeToJail = Time -- TIME TO JAIL VARIABLE

when i traced the errors, it means that in

function JailModule.Jail(plr, Time, reason)

when you called the function, the second argument Time you gave, was nil.
Try checking in your scripts if there is a typo

1 Like

It isn’t supposed to be nil, because in another script there is this:

		local TimeToJail = 60*Time
		JailModule.Jail(player, TimeToJail, reason)

Definitely don’t use tick. Tick is subject to timezones and local time. os.time is what you want in this case.