How would I detect if the player plays the game the next day?

Say a player joins the game at 19.00, then at 5.00 joined the game after waking up, here’s what I tried.
The LastPlayed value is in Unix time.
Bascially I want it to detect whether player has joined after 4.00 every day BUT if the player last joined at time at most 4 hours earlier than that, wait until 8.00 and if the player didn’t join between the day range ignore it too.

local Now = os.time();
-- 4 hours and day starting at 04.00, BUT if the user last played at time is 4 hours closer then that 04.00, ignore it until 8.00, if the player joined the day after, ignore it too.
if Now - LastPlayed < 14400 then
	return false;
elseif Now - LastPlayerd > 172800 then
	return true;
elseif ((LastPlayed - 14400) % 86400) > ((LastPlayed - 14400) % 86400) then
	return true;
end;
1 Like

Never done this before so I might go on a limb with a guess here. After researching a bit I came to the conclusion that you can try using DataStore and os.time() to check for it. You can try checking out some of the daily rewards system to help you derive a script from it.

Example is -

This is as far as I could go in terms of helping with this. Hope it helps.

2 Likes

No not every 24 hours. Every time we consider a day. There’s no constant hour for this.

maybe you could use tick() command.

What would that change?
This is also for Lua 5.3 so tick() won’t work.

I already got the data of the player I’m looking for detecting if player joined what we consider next day (not 24 hours) by the two dates given.
Also I’m porting this to Lua 5.3 too.

1 Like

use os.date(“*t”)

local currentDay = os.date("*t").day -- that's the day they logged in

check if they’re not equal and that should do the job. (the current os.date(“*t”).day and the one they have saved)

I’ve took the algorithm a shot at this again

const int hour_offset = 14400;
int ts1 = timestamp1 - hour_offset - tz_offset;
int ts2 = timestamp2 - hour_offset - tzoffset;
if (ts1 / 86400 > ts2 / 86400)
{
	return ((ts2 - ts1) / 86400) < 2;
}
else
	return false;

As it’s unpredictable what we actually consider a day. e.g. Some wake up at 11.00 while others wake up at 05.00 it’s best to consider the start of the day at 4.00, actually doing it is too much work I’m not going to do.

1 Like

Again, you can’t really get the “next day” for all machines, it’s best to stick with UTC so you don’t get “time machined” (they can just change the local machine time and skip a day).
Lua 5.3 has os.date(“*t”) and so does Lua 5.1.

Rather than doing math for no reason at all why don’t you just use that?

However, what you can do is ask for for their timezone and compensate for that when checking for the day but, if this is an online game it’ll just be a huge mess.

Not really. There are two different approaches of solving this issue that I’ve seen:

  1. Basing it on the utc timezone (os.time/os.date) and/or reducing the time from 24 hours to i.e. 18 hours to create a small window allowing to “shift” the day cycle, if that makes sense
  2. Starting the 24h timer the first time the player joins the game or collects the reward after breaking the streak

In my opinion the first one is the best. If you’ve ever played minecraft you might know that Hypixel does exactly that ^^

1 Like

Australia is 12 hours ahead, I don’t want it to detect as 12.00 for Australia users.
I prefer working in unix time. os.date seems redundant for this. Also I don’t want to detect if the player joins the day after. I’ll implement a timezone detector and check if the user has changed the time.

I mean actually detecting for when’s a person waking up and what considered a day for a person.

And I qoute : " I’m looking for detecting if player joined what we consider next day ( not 24 hours) by the two dates given."
How is os.date “redundant” for this? It couldn’t be any better for this specific use, it won’t check if “24 hours” passed, it’ll check if a day has passed, yes that means if they logged in at 11:59 and left, then relog at 12:01 or 12:00 it would be a different day (which is exactly what you said you’re trying to do).

Could you please explain what you’re “REALLY” trying to do if what I said wasn’t actually what you meant?

At the end it doesn’t matter whether I use os.date or floor diving it by 86.400.
Look at this:

local a = os.date("*!t", ts1);
local b = os.date("*!t", ts2 - 86400);
return (a.year == b.year and a.month == b.month and a.day == b.day);

As opposed to this:

return ts1 // 86400 == (ts2 // 86400) - 1
print(os.date("%d")) -- prints current day
print(os.date("%H")) -- prints current hour

You can use these (or some more) to make it (I think)
(Yes I know that its been so much time lol)