Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
8

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

Asked by
sgsharp 265 Moderation Voter
9 years ago

This question has been solved.

0
Thanks! I really appreciate your help! sgsharp 265 — 9y

Locked by NinjoOnline, Spongocardo, and EzraNehemiah_TF2

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
6
Answered by 9 years ago

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 scriptinside a TextLabel

LastLogindefaults 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

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 BlueTaslem 18071 — 9y
0
Thanks to the both of you! I'll be testing and working with this soon. I hope all goes well! sgsharp 265 — 9y
1
I might add this to my game too :) Nice and neat and simple to read and understand. +1 NinjoOnline 1146 — 9y
0
Quick question... How could I incorporate this into a GUI? I want the player to see when their next reward will be. sgsharp 265 — 9y
View all comments (2 more)
1
os.time - Lastlogin (secounds to next time) you could also /60 to get mins and another /60 to get hours RM0d 305 — 9y
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)? sgsharp 265 — 9y
Ad