Wait for a specified amount of time in real life

Thank you, but what is the “+ 8” for? I can’t understand

Example. You want to jail the player for 120 seconds

The current time will be tick()

and the time that prisoner can be free is tick() + 120

1 Like

So I have this script, but as far as I see from the print it doesn’t change the time value at all.

function CountTime(plr, Time)
	print("start count")
	local jail = true
	local timeToFree = tick() + Time -- jail time in seconds
	
	while jail do
		print(timeToFree)
		wait(1)
	end
	if tick() > timeToFree then
		print("unjailed")
		JailModule.Unjail(plr)
		jail = false
	end
end

i think you forgot to put the if statement inside the loop. Should be

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

this won’t stop since

jail is true and you didn’t chang it to false, until here

but of course this won’t run since while loop is still running

1 Like

Alright thank you @Lielmaster and @ronald, I’ll continue working on the script and tell you when I will need more help in terms of saving time when the player leaves the game.

1 Like

just a question, should the player be in game for the time to go down? if so then there are other things you have to do

1 Like

feel free to dm me if you need any help.

1 Like

Yes, he’s supposed to be in game for the time to go, and when he leaves the time saves until the player gets back in game, then continues working, etc.

Wait. So that means it doesn’t count if player is not in game?

1 Like

It does.
30charactwersssssssssssssss

in this case you need to save timeToFree - tick() instead of timeToFree. By doing this, you save the time left before they are free, and when they join in, get the tick() and add the number that you saved and that will be the new timeToFree

1 Like

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)