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
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.