# How to make a afk rewards like clash of clans

like in clash of clans, there is gold collector, gem collectors which give u gold/gems when u are out of the app or in it. May i know how to make one of that (i want to mainly make a system to see how long a player leave the game before the player join back)

thanks guys

What you could try is to save the time the player left (probably using os.time and datastoring), then when the player joins, get the saved time and subtract it from the time they joined, youâ€™ll have the time the player was out of the game, then you calculate how much they wouldâ€™ve gotten had they stayed in game by checking how much they make every second or how much time it takes to get resources and multiplying it with the time it took for them to come back to the game

2 Likes

can i use os.difftime to compare the two time???

I believe you can do that as well, I suggested subtraction since thatâ€™s what I used mostly, so long as you have the time they left and the time they joined again youâ€™ll be able to get the time that they werenâ€™t in the game

How to use subtraction???
thanks for ur quick response

Hereâ€™s an example

``````-- Code above where you get the time they left from a datastore
local timeNotInGame = os.time() - leaveTime
-- We do os.time() here to get the time they rejoined
-- Doing leaveTime - os.time() will give us a negative number because
-- os.time() will always be bigger than our leaveTime
``````

ok thanks =D
will the reutrn value be in seconds or what???

Thatâ€™s correct, `os.time()` returns the amount of seconds that have passed since January 1st, 1970 00:00:00, if thatâ€™s not how frequent you give materials, you can do some simple division on the resulting time,

Letâ€™s say you already got the seconds since they left and came back, if you give materials every 30 seconds, divide the resulting time by 30

1 Like