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

Login Bonus not working?

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

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.

10 comments

0

Thank you very much, but it just says "Time until next reward:d:d:d" Any ideas? :D :D Thanks!

0

You need a percentage symbol before each of the d's. Edited my answer.

0

This is still not working :/ Any ideas?

1

Edited answer.

0

Thanks so much! I will check it out! :)

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

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.

0

To do that (reset it) would I do this local LastLogin = DataStore:GetAsync(Player.userId) or os.time() under the else?

0

(I am already awarding the points in the main script in the workspace, so that is fine!)

0

You could just do local LastLogin = os.time() and then save it if you want.

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