• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Calculate Consecutive Login Days

ohman

Member
Joined
Oct 24, 2008
Messages
294
Reaction score
8
Location
Sweden
What is the best way to calculate the number of consecutive days a player has logged in? Logging the date, etc., seems complicated considering leap years, new years, and so on. The idea is to use this for a daily reward system via a login event. Thanks!
 
Use the difference between the current date and the last login date.
If the difference is exactly 1 day, increment the streak.
If the difference is greater than 1 day, reset the streak to 1 (since the player missed one or more days).
If the difference is 0 days (the player is logging in multiple times on the same day), do nothing.

Use a storage value to save the value and increment it.

--
If a player knows the exact time this resets (midnight), they could login at 11:59pm, and then logout, login at 12:01am, and get 2 days at a time..
but that's just kinda how daily logins end up working out. 🤷‍♂️
 
What is the best way to calculate the number of consecutive days a player has logged in? Logging the date, etc., seems complicated considering leap years, new years, and so on. The idea is to use this for a daily reward system via a login event. Thanks!
To further explain what Xikini said:
lastlogin is stored as a unix timestamp, so it doesn't care about timezones/leap years etc. Just get the difference and if its less than 86400 seconds, increment, otherwise reset. Or as Xikini says, you can do it actually "daily" and find the day from the timestamps for both current and last.
 
Thank you for your responses!
Just one question. If I extract the day of the year, for example, 239 which is today, it's easy to check if it's the next day or if a day has been missed. But what happens on January 1st? From 365 to 1, the the streak is broken? The difference is bigger than 1.
Or am I wrong?
 
Thank you for your responses!
Just one question. If I extract the day of the year, for example, 239 which is today, it's easy to check if it's the next day or if a day has been missed. But what happens on January 1st? From 365 to 1, the the streak is broken? The difference is bigger than 1.
Or am I wrong?
Yes.. and no.

Instead of doing that, I would just count the total amount of days.

Something like this..?
Untested.
LUA:
local storageConsecutiveDays = 10001 -- storages to hold information
local storageLastLoginDay = 10002

function onLogin(player)    
    local currentDay = math.floor(os.time() / 86400) -- calculate the current day #
    local lastLoginDay = player:getStorageValue(storageLastLoginDay)
    local consecutiveDays = player:getStorageValue(storageConsecutiveDays)
    
    -- if first time Login, these if's set initial values
    if lastLoginDay <= 0 then
        lastLoginDay = currentDay - 1 
    end
    if consecutiveDays <= 0 then
        consecutiveDays = 1
    end
    
    -- calculate day difference
    local dayDifference = currentDay - lastLoginDay
    consecutiveDays = dayDifference == 1 and consecutiveDays + 1 or dayDifference == 0 and consecutiveDays or 1 -- check to see if we want to add a day, do nothing, or reset
    player:setStorageValue(storageConsecutiveDays, consecutiveDays)    
    player:setStorageValue(storageLastLoginDay, currentDay)
    return true
end
 
Back
Top