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

Help with daily login reward script?

Let me say first, it basically works fully, but their is a minor error.

ServerScript (Inside ServerScriptService)

game.Players.PlayerAdded:connect(function(player)
    local LastLogin = loginstore:GetAsync(player.userId)

    if LastLogin then
        local seconds = os.time() - LastLogin
        local minutes = seconds/60
        local hours = minutes/60
        if hours >= 24 then
			loginstore:SetAsync(player.userId, os.time())
        end
    else
        loginstore:SetAsync(player.userId, os.time())
    end
end)

ServerScript (Inside a TextLabel)

local rewardtime = 24*60*60

local loginstore = game:GetService("DataStoreService"):GetDataStore("LastLogin")
local player = script.Parent.Parent.Parent.Parent.Parent
local lastlogin = loginstore:GetAsync(player.userId) or os.time()

while true do
	Seconds = rewardtime - (os.time() - lastlogin)
	Minutes = Seconds/60
	Hours = Minutes/60	
	script.Parent.Text = ("Daily Reward: d:d:d"):format(Hours,Minutes%(60),Seconds%(60))
	wait(1)
end

Ok, so this works, as I said, with the whole counting down, etc. But if a Player joins and the hours is > 24 then it prints("Reward"), but the text at the bottom says "Daily Reward: -1:35:54" As you can see, it says -1, which is should be like "Daily Reward: 23:59:59" and then start to countdown.

When the player leaves and rejoins, it's working properly again, but I want it to say 23:59:59 and countdown when they get the reward.

Please help!

1 answer

✓ Accepted answer

Check your seconds
And your privileges

If Seconds is 0 or less, it's payday. And then we can safely count down again.

local rewardtime = 24*60*60

local loginstore = game:GetService("DataStoreService"):GetDataStore("LastLogin")
local player = script.Parent.Parent.Parent.Parent.Parent
local lastlogin = loginstore:GetAsync(player.userId) or os.time()

while true do
while true do
    Seconds = rewardtime - (os.time() - lastlogin)
	if Seconds <= 0 then break end; -- PAYDAY FELLAS
    Minutes = Seconds/60
    Hours = Minutes/60  
    script.Parent.Text = ("Daily Reward: %.2d:%.2d:%.2d"):format(Hours,Minutes%(60),Seconds%(60)) -- FTFY
    wait(1)
end
loginstore:SetAsync(player.userId, os.time());
lastlogin = os.time();
end

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