How to make an offline earning system?

How would I make a system that makes you earn money, while you are offline?

I have absolutely no idea where to even start this, I looked up YouTube videos and found nothing.

Thanks for any help!

2 Likes

you can determine how much time has passed while a player was offline by using os.Time if you log it when they leave and join

5 Likes

tick() or workspace:GetServerTimeNow() and just compare it with time when player logs in

1 Like

os.time is outdated
just use tick() at this point

1 Like

I would recommend you learning DataStoreService, and the better alternative: ProfileStore, which would allow you to familiarize yourself with this stuff.

I personally haven’t used DataStores (I use ProfileStore instead), so my DataStore lines may be a bit shaky, but here’s something that I drew up in 2 minutes.

-- Use data store service to get the time the player left at

local name = "myDataStore"

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local myStore = DataStoreService:GetDataStore(name)

Players.PlayerRemoving:Connect(function(plr)
	local currentTime = os.time() -- or: DateTime.now().UnixTimestamp
	
	myStore:SetAsync("LastPlayed"..plr.UserId, currentTime)
end)


Players.PlayerAdded:Connect(function(plr)
	local currentTime = os.time() -- or: DateTime.now().UnixTimestamp
	
	local previousTime : number = myStore:GetAsync("LastPlayed"..plr.UserId)
	
	local secondsSinceLastJoin = currentTime - previousTime
	
	local minutesSinceLastJoin = currentTime / 60
	
	local coinsEarned = minutesSinceLastJoin * 3
end)
2 Likes

That just a datastore wrapper :skull:

1 Like

Yeah, that’s common knowledge.
I mentioned it b/c the syntax differs.

2 Likes

game:GetService("DataStoreService") my friend

It’s basically the first 2 responses, but they both kind of just tell you the first step and assume you’ll piece together the next so I’ll include the next steps in case you don’t quite understand.

Once you have the time passed since they have been offline you need to in some way similulate that time passing in your game. The most efficient way is to treat everything like a function.

For example, say they make $30 a minute.
First we’ll change this to per second since that’s how you’ll get time measured with tick() and we need to be working with the same units. So $30/60 is $0.5 per second. Then you just multiply that by the seconds that have passed while they were offline and you have the amount of money they would have earned in that time.

Another way would be to simulate the major checkpoints they would cross in that time but this will be slower and you should definitely cap it so you aren’t processing 30 years for a player who randomly decides 30 years later to rejoin. This is useful for things you can’t define a simple function for like above, but the above method is the preference.

There are technically other solutions but at some level they are just more complicated versions of the above (which some could argue are just the same thing anyways). For example think like if money is a random occurrence in rate, value or both you can just capture the average over a time frame and do method 1.

5 Likes

Do not do this, tick can be inconsistent across time zones and operating systems as stated in the docs. Tick is more suited for timing shorter things such as a debounce.

3 Likes

With the combination of the previous developer’s opinions, here is how i would do it:

Whenever the player logs off, use datastore to save the data he logged off, you can use (and should) use Universal Time
Then you would do the maths, like @tlr22 Example.

Overall use the api if possible like DateTime and do the maths correctly

1 Like

correct me if I’m wrong, but I think that tick is more unreliable than os.time as os.time uses a unified timezone while tick does not
in all cases it’s better to use DateTime for now until it also gets deprecated :derp:

1 Like

This is not good practice btw. Don’t use a whole sperate key for a single value.

ohh so i should save them in a table?

Yeah. Have a key for each player, not a key for each statistic.

Hello everyone, and thanks for replying! I have not had time to look through the replies yet, but thank you for the scripts, and advice. I’m going to be going through, and be adding them to my game soon, thanks!