Help with os.difftime()

I’m trying to calculate the time difference beetween 2 times using Difftime() os | Roblox Creator Documentation but to do this I need to format in “time_t format” http://en.cppreference.com/w/cpp/chrono/c/time_t

My code is:

		local Time1 = os.clock()
		wait(math.random(1,60))
		local Time2 = os.clock()
		local WaitTime = os.difftime(Time1,Time2)
		print(WaitTime)

All help is appreciated.

2 Likes

You can just subtract “Time1” from “Time2” and you’ll get the time that has elapsed.
Example:

        local Time1 = os.clock()
		wait(math.random(1-60))
		local Time2 = os.clock()
		local WaitTime = Time2-Time1
		print(WaitTime)
2 Likes

I get an error
image
Any ideas?

1 Like

Can I see your script because that shouldn’t happen since os.clock() returns a number

1 Like

The one where this is being used?

1 Like

the script that caused the error

1 Like
local Event = game:GetService("ReplicatedStorage"):WaitForChild("Technica_Replicated"):WaitForChild("Technica_Remote")
local RunService = game:GetService("RunService")


local Plr_Recording
local Ref = 0
local Cue_Start_Time
local Cue_End_Time
local New_Key_Time
local Prev_Key_Time
local Temp_Wait_Time
local Recording_Key_Press = false
local Cue_Total_Length
local first_run

return  {
	Start_Recording = function(Plr)
		warn("Sarting Key Press Recording")
		spawn(function()
			first_run = true
			Recording_Key_Press = true
			Plr_Recording = Plr
			Temp_Data = table.create(1)
			Ref = 0
			Cue_Start_Time = os.time()
			print(Cue_Start_Time)
			RunService.Stepped:Connect(function(step)
				Event.OnServerEvent:Connect(function(Plr,Query1,Query2)
					if Recording_Key_Press == true then
						New_Key_Time = os.time()
						print(New_Key_Time)
						if first_run == true then
							Temp_Wait_Time = New_Key_Time-Cue_Start_Time
							first_run = false
						elseif first_run == false then
							Temp_Wait_Time = New_Key_Time-Prev_Key_Time -- I get an error here
						end
						if Plr == Plr_Recording then
							Ref = Ref + 1
							local Key_Data = {
								Ref,
								Temp_Wait_Time,
								Query1,
								Query2
							}
							print(Key_Data)
							Temp_Data = table.insert(Key_Data)
							Prev_Key_Time = os.clock()
							print(Prev_Key_Time)
						end
					end
				end)
			end)
		end)
	end,
	End_Recording = function(Plr)
		warn("Finished Key Press Recording")
		Cue_End_Time = os.time()
		print(Cue_End_Time)
		Recording_Key_Press = false
		Cue_Total_Length = Cue_Start_Time-Cue_End_Time
		print(Cue_Total_Length)
		print(Temp_Data)
	end,
}
1 Like

can you add a comment to the line the script errored?

1 Like

It seems that “Prev_Key_Time” was never initialized

image
But it is… thats why im confused

That is because it was declared after the line where it errored

But for it to work as intended it has to go there or else I can not calculate the exact time to wait. Unless there is better way to do it.

You initialize it with a default value, possibly ‘0’ to prevent the error.

1 Like