Why Won't Os.Time Work?

What I am making is that when you click a button, it’s going to print how much time as passed since you last clicked the button. But it won’t work:

local button = script.Parent.TextButton


button.MouseButton1Up:Connect(function()
	
	while wait(1) do
		
		local currentTime = os.time()
		
		print(currentTime .. " seconds have passed since you last clicked the button")
		
	end
end)

Output: image

2 Likes

OS.Time() returns a unix epoch timestamp, you’ll need to convert that!


Answering your question, you’ll also have to store the time of the last click, OS.Time() just returns the current time. You can then work the difference out between them. :slight_smile:

3 Likes

It works as it should be, you just need to set a value to the time when you click the button, and in while loop you will do starttime - os.time() and this will give you the time since you pressed the button

1 Like

os.time() when run with no arguments gets the time in seconds since the UNIX Epoch(00:00:00 UTC Jan 1st 1970)
To get the time you need to store the last time they clicked in a variable and subtract that from the current time
Ex

local lasttime = os.time()

button.MouseButton1Up:Connect(function()
    local timesincelastclick = os.time() - lasttime
     print(timesincelastclick)
end)
3 Likes
local PreviousClick = 0

button.MouseButton1Down:Connect(function()
  print((os.time()-PreviousClick).." seconds have passed since last click")
  PreviousClick = os.time()
end)

Im too sure about this but this could work.

4 Likes

This happens: image

Which script are you using? How often are you clicking the button?

The script is in starter gui inside a screen gui.

Yeah, but what script are you using? Can you show me the updated script please?

I tried this and it worked:

local button = script.Parent.TextButton
local startingTime = os.time()



button.MouseButton1Up:Connect(function()

	while wait(1) do

		local currentTime = os.time()

		print(currentTime - startingTime .. " seconds have passed since you last clicked the button")

	end
end)

But I don’t understand the logic behind of it.

Ok. So above you are defining the button and getting the starting time. When the button gets clicked, you are waiting in a while loop. In that loop, you are getting the current time and then printing the difference between the time, which returns seconds.

1 Like

And does the startingtime variable give you the time in which the server started running?

Since the script began to run. It is defined at the start of the script.

1 Like

Ok but there’s a problem. If I wait a couple of seconds. Then click the button. It prints the amount of seconds that the server started to run. For example, if I spawn in the game and wait about 3 seconds then click the button. The output’ll say 12 seconds. And 12 seconds is when the server started to run. image

it starts counting the moment the script runs, which is in your case when the server starts.

Ok that’s the issue with the script, which went against what I was trying to advise in reply #1. You want to store in a variable when you last pressed the button OR when the server opened if the previous one is null.

So would it be like this?

local button = script.Parent.TextButton



button.MouseButton1Up:Connect(function()
	local startingTime = os.time()
	
	while wait(1) do

		local currentTime = os.time()

		print(currentTime - startingTime .. " seconds have passed since you last pressed the button!")

	end
end)
1 Like
local PreviousClick 
local FirstTime = false

button.MouseButton1Down:Connect(function()
  if not FirstTime then FirstTime = true PreviousClick = os.time() end
  print((os.time()-PreviousClick).." seconds have passed since last click")
  PreviousClick = os.time()
end)

Possible something like this, I just added on to the first reply.

Please give me a moment to load studio so I can bug-test my code. I’ll confirm and edit this post when confirmed.

Edit: @TheDCraft 's reply above looks correct, and was what I was suggesting above in reply 1 to store the time of the last click in a value.

1 Like

That is indeed the code that will count the time from when the button was clicked rather than when the script started running. Good job

1 Like