Why Won't Os.Time Work?

I think it will always make 0 wouldn’t it?
Since every time you click it would get reset to the current time although I am not too sure.

No, cause the startingTime variable gets set when the player clicks the button. The counting is all done in the while loop where the currentTime variable gets changed every second. The startingTime variable doesnt, until the player clicks the button again.

1 Like

I’d beg to differ, every time the button is clicked, it’s resetting the value to the current time.

Oh right, I misunderstood the whole scenario. I though it meant to find out the the last time you clicked, instead of showing the how long it’s been since the last press. My bad. I completely agree with you.

1 Like

I said that. The startingTime variable gets set to the current os.time() when the player clicks the button, but doesnt change over time, like the currentTime variable does in that loop

Ah alrighty, I see where it is, I mis-read the question with TheDCraft.

1 Like

@Mysterious_Myth11 although the code you gave is technically correct, it’s got a few issues. If you click it more than once, it’ll count up multiple times - for example “1, 10, 2, 11, 3, 12” if you waited 10 seconds between each click.

This version will only count from the first click, it will ignore everything after.

local button = script.Parent.ClickDetector
local logging = false

button.MouseClick:Connect(function()
	if logging == false then
		local startingTime = os.time()
		logging = true

		while wait(1) do

			local currentTime = os.time()

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

		end
	end
end)

lol it did work.
it is returning how many seconds have passed since January 1970. to find out how much time passed subtract the new os.time() from the old os.time()