Hiii, Currently i want to do a private game about my birthday, Celebrating Every year!, But I don’t know how i could do the logic, let’s say every feb 3 , every year, A Specific Age number goes up, and saves for the next year, and so on, how could i implement this?, would i need to use a DataStore?
You probably won’t need to use DataStore
’s, you could try setting the year you we’re born and just making it add up the age every year that passes.
You just need to get the current year and just subtract it from the day u were born and there’s your age.
To make something like that, you can use DateTime.now()
to get the current date and time. From that, you can check if the date is February 3. You’ll also need a DataStore to save the age so it persists between sessions.
Here’s a short explanation how you could do it, there are more ways how you can do it tho, including ones where you don’t need to use a datastore.
- Use
DateTime.now()
to get the year, month, and day. - When you join the game, check if the current date is February 3 and if the year is different from the last time your age was updated.
- If it is, increase your age by 1 and save the new age and year in a DataStore.
Hey. From what I understood, you’re trying to make a game specifically for your birthday, right? If so, you don’t need to use datastores or anything, as your birthday remains constant and you can just calculate how old you are.
math.floor(os.difftime(os.time(), os.time({year=####,month=##,day=##})) / os.time({year = 1971, day = 1, month = 1}))
The code above calculates your age if you just fill in the hashtags with the values for your birthday. It first gets the seconds that passed from your birthday until now, then it divides that by the seconds in a year (1970-1971 = 1 year), and then you get how many years have passed since your birthday. But then, since it spits out a floating point integer, you use the math.floor()
function to reduce it to just the amount of years.
I hope I understood your question correctly. If not, feel free to correct me.
I guess I could Get the Current Age By Using @neviquatro Solution, And Save it Doing what @LiamSternenstei1 Said, So, thank you both
Looks Solid!, I’ll use this then, Thanks!, At the end it was just doing some math ha
Yeah, it often is, haha.
I’ll give you a more detailed explanation of it here, if you or anyone would like it:
local birth_year = 2000
local birth_month = 1
local birth_day = 1
-- current seconds since 1970 - (minus) seconds from 1970 to birthday = seconds since birthday
local seconds_since_birthday = os.difftime(os.time(), os.time({
year = birth_year,
month = birth_month,
day = birth_day
}))
local seconds_in_a_year = os.time({year = 1971, day = 1, month = 1})
local years_since_birthday = seconds_since_birthday / seconds_in_a_year -- still a float
local rounded_years_since_birthday = math.floor(years_since_birthday) -- end result
print(rounded_years_since_birthday)