Best way to store player join date and total time played

I wanna store the players join date (on my game, just the year, month and day) as well as keep track of their play time in seconds. Here’s what I got so far

Joined = {
	Year = os.date("*t", os.time()).year,
	Month = os.date("*t", os.time()).month,
	Day = os.date("*t", os.time()).day,
},

PlayTime = 0,

And then my problem comes with tracking the total play time. For example, when a player joins

-- Setup extra values
local JoinTime = Instance.new('IntValue')
JoinTime.Name = 'JoinTime'
JoinTime.Value = os.time()

and then when they leave

-- Set PlayTime
PlayerData.PlayTime = PlayerData.PlayTime + os.difftime(os.time(), player.JoinTime.Value)

Is this the most efficient way of doing all this?

3 Likes

Seems efficient, your program’s upper bound run-time complexity is defined by the time and date functions. And time() returns a number, so it has pretty low space cost too. If anything I would have the variable joined as something like:
Joined = (os.time{year=os.time()).year, month=os.time()).month, day=os.time()).day})
That way its stored as a number rather than table.

Overall great job, I think it’s executed real well. You might also want to look at the BindToClose() method (if you haven’t already), when updating values to a datastore for when the server shuts down after last player leaves.

2 Likes