Da_Fr0st
(Frost)
#1
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
sata5pa3da
(sata5pa3da)
#2
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
Da_Fr0st
(Frost)
#3
I get an error

Any ideas?
1 Like
sata5pa3da
(sata5pa3da)
#4
Can I see your script because that shouldn’t happen since os.clock() returns a number
1 Like
Da_Fr0st
(Frost)
#5
The one where this is being used?
1 Like
sata5pa3da
(sata5pa3da)
#6
the script that caused the error
1 Like
Da_Fr0st
(Frost)
#7
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
sata5pa3da
(sata5pa3da)
#8
can you add a comment to the line the script errored?
1 Like
sata5pa3da
(sata5pa3da)
#9
It seems that “Prev_Key_Time” was never initialized
Da_Fr0st
(Frost)
#10

But it is… thats why im confused
sata5pa3da
(sata5pa3da)
#11
That is because it was declared after the line where it errored
Da_Fr0st
(Frost)
#12
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.
Forummer
(Forummer)
#13
You initialize it with a default value, possibly ‘0’ to prevent the error.
1 Like