Hi! So I’ve been working on a Daily Reward Chest but recently I’ve ran into an issue I’ve been trying to display the time on a billboard gui text. It would work for players that have data but not for new players, I tried looking everywhere asked people for help/ looked in devforum and no luck so far. Here is my code so far
local DS = game:GetService("DataStoreService"):GetDataStore("DailyReward0.001")
local RS = game:GetService("RunService")
local hourWait = 24
local function toHMS(s)
return string.format("%02i:%02i:%02i", s/60^2, s/60%60, s%60)
end
game.Players.PlayerAdded:Connect(function(player)
local HoursWaitInSeconds = hourWait * 3600
local timeNow = os.time()
local data = os.time()-1000
local NextClaimTime
pcall(function()
data = DS:GetAsync(player.UserId.."-dailyReward")
end)
if data ~= nil then
local function LoopUpdate()
while wait(1) do
timeNow = os.time()
print(toHMS(NextClaimTime - timeNow))
script.Parent.Touch.Info.Desc.Text = "Claim again in: "..toHMS(NextClaimTime - timeNow)
end
end
NextClaimTime = data + HoursWaitInSeconds
if NextClaimTime <= timeNow then
script.Parent.Touch.Info.Desc.Text = "CLAIM"
local reward = math.random(5,20)*10
local connection
connection = script.Parent.Touch.Touched:Connect(function(triggeringPlayer)
if triggeringPlayer == player then
game.ReplicatedStorage.GameClient.Remotes.DataUpdate2:FireClient(player, reward, "TokensStore")
DS:SetAsync(player.UserId.."-dailyReward", os.time())
connection:Disconnect()
end
end)
LoopUpdate()
else
--print("plr can't claim reward atm")
LoopUpdate()
end
else
print("new Player")
local reward = math.random(5,20)*10
local connection
connection = script.Parent.Touch.Touched:Connect(function(triggeringPlayer)
if triggeringPlayer == player then
--print("Reward Claimed")
game.ReplicatedStorage.GameClient.Remotes.DataUpdate2:FireClient(player, reward, "TokensStore")
DS:SetAsync(player.UserId.."-dailyReward", os.time())
connection:Disconnect()
end
end)
script.Parent.Touch.Info.Desc.Text = "CLAIM AGAIN IN 24 HOURS"
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data
pcall(function()
data = DS:GetAsync(player.UserId.."-dailyReward")
end)
if data == nil then
pcall(function()
local timeVal = os.time()
DS:SetAsync(player.UserId.."-dailyReward")
end)
end
end)
Please let me know if I did something wrong thanks!