Getting time past from 2 os.clock()

I am making a daily quest system, and it is very buggy.

The logic i got is from: How to make a Daily Reward System? | Tutorial

But what is the way i can make a timer from that?

local numTime = player.numTime

nextRefresh = (numTime.Value - os.time()) + (246060)

and this creates negative numbers
image

and i need it to be:
image

(never worked with os.clock() before, any suggestions?)

os.time() returns how many seconds have passed since the Unix epoch (1 January 1970, 00:00:00)
knowing this, you’ll have to convert it into hours, minutes, and seconds for your timer: (assuming your numTime variable is in unix epoch time)

-- Rearrange this because in most cases, the os time will be greater than your variable
local nextRefresh = os.time() - numTime.Value

-- Convert to hours
nextRefresh / 3600

-- Convert to minutes
nextRefresh / 60

-- Convert to seconds
nextRefresh

If you need anything else, don’t be afraid to ask! :]

2 Likes

image

it started looking like that and if i rearrange it will negative

i am trying to create a timer that will count 24 hours to the next day and the first os.time() start from the value then player joined and got daily quests

i got a little bit closer
image
but it still not the thing i really wanted to do

local numTime = player.numTime

        nextRefresh = os.clock() - numTime.Value
        
        nextRefresh = nextRefresh + (24*60*60)

How is the numTime value being set? It may not be in UTC time.

1 Like

well, according to the tutorial you linked, here is what the code they have for this: (modified)

if tonumber(nextRefresh) < os.time() - 86400 then -- (86400 seconds = 24 hours)
    -- Your code here
end

have you tried using this?

game.Players.PlayerAdded:Connect(function(player)

    local foundData = DailyRewardDataStore:GetAsync(player.UserId)
    
    local numTime = Instance.new("NumberValue",player)
    
    numTime.Name = "numTime"
    
    if foundData then
        
        if tonumber(foundData) < os.time() - 86400 then
            
            local cur = os.time()

            DailyRewardDataStore:SetAsync(player.UserId,cur)
            
            module.CreateQuests(player)
            
            numTime.Value = cur
            
        end
        
    else
        
        local cur = os.time()
        
        module.CreateQuests(player)
        
        DailyRewardDataStore:SetAsync(player.UserId,cur)
        
        numTime.Value = cur
        
    end
    
end)

Try replacing all uses of os.time() to workspace:GetServerTimeNow()?

image

looks like it worked, but i’m worrying about this, will this work?

also what is the diffrence between getservertimenow and os time

after rejoining it turns into this
image

According to the docs, GetServerTimeNow returns the epoch time on the server with microsecond precision. and with your problem of the -478098 your saved variable may not be using this new Workspace:GetServerTimeNow variable.

Finally, just fixed that. The problem was that is was setting value of numTime only the time you join and you have no data or you was online last time more than 24 hours, i fixed by adding else

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.