Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Player Login Rewards: How is os.time() implemented?

This question has been solved.

1 comment

0

Thanks! I really appreciate your help!

1 answer

✓ Accepted answer

You could save the time of their last login in datastore and then see how much time has passed.

local DataStore = game:GetService("DataStoreService"):GetDataStore("LastLogin")


Game.Players.PlayerAdded:connect(function(Player)
	wait()
	local LastLogin = DataStore:GetAsync(Player.userId)
	if LastLogin then
		local Seconds = os.time()-LastLogin
		local Minutes = Seconds/60
		local Hours = Minutes/60
		print("It has been: "..Hours.." hours Since your last reward")
		if Hours >= 24 then
			print("You get a daily login award")
			DataStore:SetAsync(Player.userId,os.time())
		end
	else
		DataStore:SetAsync(Player.userId,os.time())
	end
end)

Script (I cannot post the script here as the site breaks it............)

The script is for a GUI. You can change the wait time in the loop to change the update rate. Or you can remove the loop as it will update whenever the player dies.

Just put this in a script inside a TextLabel

LastLogin defaults to os.time() in case the player has no value in DataStore. This should not happen because a value is assigned when a player joins the game for the first time but it does not hurt.

To find the TimeLeft I subtract the time since the players last reward by 24. I find the TimeLeft using the same method as before: subtracting from the current time and then dividing by 60 twice. It is just condensed into one statement instead of using multiple variables. Then I used some fancy string formatting and math to display the time left as a clock would.

Edits: Edited to not save when the player leaves :) Updated to show time in the correct format! Updated to show time in minutes and not a percentage Updated to be more efficient Updated to support Seconds

6 comments

5

This exact code will punish players for playing more frequently than 24 hours -- you want to save the time when they receive the bonus, not when they quit

0

Thanks to the both of you! I'll be testing and working with this soon. I hope all goes well!

1

I might add this to my game too :) Nice and neat and simple to read and understand. +1

0

Quick question... How could I incorporate this into a GUI? I want the player to see when their next reward will be.

1

os.time - Lastlogin (secounds to next time) you could also /60 to get mins and another /60 to get hours

0

Okay, the scripts work, but the TextLabel is saying "11.4" How do I get it to say 11:40:00 (Hours Minutes, Seconds)?

This discussion is preserved as it was. New answers, comments, and votes are closed.