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)
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.
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
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)
local PreviousClick = 0
button.MouseButton1Down:Connect(function()
print((os.time()-PreviousClick).." seconds have passed since last click")
PreviousClick = os.time()
end)
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)
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.
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.
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.
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)
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.