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

Login Bonus not working?

Asked by 8 years ago

Hi guys! My login bonus awards players if they have come back after 24 hours or more, however the countdown gets messed up. For example, after the 24 hours is up it starts doing minus hours (For example, "-47:30:29")

Any ideas? Thanks.

Main Script in Workspace:

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

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 in TextLabel:

local RewardTime = 24*60*60

local DataStore = game:GetService("DataStoreService"):GetDataStore("LoggedIn")
local Player = script.Parent.Parent.Parent.Parent
local LastLogin = DataStore:GetAsync(Player.userId) or os.time()

while true do
    Seconds = RewardTime-(os.time()-LastLogin);
    Minutes = Seconds/60;
    Hours = Minutes/60; 
    script.Parent.Text = ("Time Until Next Reward: d:d:d"):format(Hours,Minutes%(60),Seconds%(60))
    wait(1)
end

1 answer

Log in to vote
0
Answered by 8 years ago

You could have an if statement in your while loop that checks if the current time minus the player's last login time is higher than the reward time, where if it is higher then it makes the time for the next reward 00:00:00.

Maybe something like this could work (this is for the TextLabel script):

local RewardTime = 24*60*60

local DataStore = game:GetService("DataStoreService"):GetDataStore("LoggedIn")
local Player = script.Parent.Parent.Parent.Parent
local LastLogin = DataStore:GetAsync(Player.userId) or os.time()

while true do
    local Seconds = RewardTime-(os.time()-LastLogin);
    local Minutes = Seconds/60;
    local Hours = Minutes/60;
    if (os.time() - LastLogin) < RewardTime then --If statement that checks if the current time taken away from the time the player last logged in is under the reward time.
        Hours = ((math.floor(Hours % 60)) >= 10) and math.floor(Hours % 60) or ("0"..math.floor(Hours % 60)) --Advanced usage of if statements. Checks if hours modulo 60 is over 10 and either sets it to the hours or adds a 0 onto the number.
        Minutes = ((math.floor(Minutes % 60)) >= 10) and math.floor(Minutes % 60) or ("0"..math.floor(Minutes % 60))
        Seconds = ((math.floor(Seconds % 60)) >= 10) and math.floor(Seconds % 60) or ("0"..math.floor(Seconds % 60))
        script.Parent.Text = "Time Until Next Reward: " .. Hours .. ":" .. Minutes .. ":" .. Seconds --Does the normal countdown.
    else --Otherwise...
        script.Parent.Text = "Time Until Next Reward: 00:00:00" --We make the countdown timer say 00:00:00, since the time has already elapsed.
    end
    wait(1)
end

I hope my answer helped you. If it did, be sure to accept it.

0
Thank you very much, but it just says "Time until next reward:d:d:d" Any ideas? :D :D Thanks! jjwood1600 215 — 8y
0
You need a percentage symbol before each of the d's. Edited my answer. Spongocardo 1991 — 8y
0
This is still not working :/ Any ideas? jjwood1600 215 — 8y
1
Edited answer. Spongocardo 1991 — 8y
View all comments (6 more)
0
Thanks so much! I will check it out! :) jjwood1600 215 — 8y
0
btw, when the time has elapsed, I just want it to count down another 24 hours - not go to 0:00:00 - because at the moment it is going to minus something. I hope this works! :D jjwood1600 215 — 8y
0
If you want to do that, for the else bit, just give them the reward you want and then just re-set LastLogin instead of going to 00:00:00. Spongocardo 1991 — 8y
0
To do that (reset it) would I do this local LastLogin = DataStore:GetAsync(Player.userId) or os.time() under the else? jjwood1600 215 — 8y
0
(I am already awarding the points in the main script in the workspace, so that is fine!) jjwood1600 215 — 8y
0
You could just do local LastLogin = os.time() and then save it if you want. Spongocardo 1991 — 8y
Ad

Answer this question