My daily reward timer is working perfectly at the moment but once someone is killing me it seems that the text resets itself and the timer is getting destroyed. So it seems that when the character dies it disconnects all its events.
Module Script Code :
local DailyRewardManager = {}
local MAXIMUM_DAILY_REWARD_TIME = 86400
local RemainingSeconds = 0
local dataTemplate = {
LastOnlineTime = 0,
TimePlayerActiveDay = 0,
TimeConnected = {}
}
local ProfileService = require(game.ServerScriptService.ProfileService)
local Players = game:GetService("Players")
local ProfileStore = ProfileService.GetProfileStore(
"DailyRewardTimer1",
dataTemplate
)
local Profiles = {}
local function playerAdded(player)
local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
if profile then
profile:AddUserId(player.UserId)
profile:Reconcile()
profile:ListenToRelease(function()
Profiles[player] = nil
player:Kick()
end)
if not player:IsDescendantOf(Players) then
profile:Release()
else
Profiles[player] = profile
local timeOnline = Instance.new("IntValue", player)
timeOnline.Name = "TimeOnline"
timeOnline.Value = Profiles[player].Data.TimePlayerActiveDay
local currentTime = os.time()
print("PLAYER HAS BEEN OFFLINE SINCE : " .. (currentTime - Profiles[player].Data.TimeConnected[1]))
print(Profiles[player].Data.TimeConnected)
if currentTime - Profiles[player].Data.TimeConnected[1] > MAXIMUM_DAILY_REWARD_TIME then
timeOnline.Value = 0
Profiles[player].Data.TimePlayerActiveDay = 0
table.clear(Profiles[player].Data.TimeConnected)
table.insert(Profiles[player].Data.TimeConnected, currentTime)
--[[local RewardGui = player:WaitForChild("PlayerGui"):WaitForChild("RewardGuiOFFICIAL")
local DailySection = RewardGui:WaitForChild("DAILY"):WaitForChild("DAILY")
local ScrollingFrame = DailySection:WaitForChild("DAILYBACKROUND"):WaitForChild("ScrollingFrame")
DailyRewardManager:ResetAllTasks(ScrollingFrame)]]
end
DailyRewardManager:RunTimer(player, MAXIMUM_DAILY_REWARD_TIME - (currentTime - Profiles[player].Data.TimeConnected[1]), "", "")
--[[while wait(1) do
timeOnline.Value += 1
Profiles[player].Data.TimePlayerActiveDay += 1
print("NUMBER OF SECONDS IS : " .. Profiles[player].Data.TimePlayerActiveDay)
end]]
end
else
player:Kick()
end
end
function DailyRewardManager:GetCurrentTime(currentTime)
return currentTime
end
function DailyRewardManager:Init()
for _, player in game.Players:GetPlayers() do
task.spawn(playerAdded, player)
end
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(function(player)
Profiles[player].Data.LastOnlineTime = os.time()
table.insert(Profiles[player].Data.TimeConnected, Profiles[player].Data.LastOnlineTime)
if Profiles[player] then
Profiles[player]:Release()
end
end)
end
function DailyRewardManager:RunTimer(player: Player, numberOfSecondsMax: NumberValue, desc: string, text: string)
game.ReplicatedStorage.RewardEvent.TimeRemaining:FireClient(player, numberOfSecondsMax, desc, text)
end
function DailyRewardManager:FormatTime(seconds)
local hours = math.floor(seconds / 3600)
local minutes = math.floor((seconds % 3600) / 60)
local remainingSeconds = seconds % 60
return string.format("%02d:%02d:%02d", hours, minutes, remainingSeconds)
end
local function getProfile(player)
assert(Profiles[player], string.format("Profile does not exist for %s", player.UserId))
return Profiles[player]
end
-- Getter/Setter methods
function DailyRewardManager:Get(player, key)
local profile = getProfile(player)
assert(profile.Data[key], string.format("Data does not exist for key: %s", key))
return profile.Data[key]
end
function DailyRewardManager:Set(player, key, value)
local profile = getProfile(player)
assert(profile.Data[key], string.format("Data does not exist for key: %s", key))
assert(type(profile.Data[key]) == type(value))
profile.Data[key] = value
end
function DailyRewardManager:Update(player, key, callback)
local profile = getProfile(player)
local oldData = self:Get(player, key)
local newData = callback(oldData)
self:Set(player, key, newData)
end
function DailyRewardManager:ResetAllTasks(scrollingFrame: ScrollingFrame)
for _, frame in scrollingFrame:GetChildren() do
if frame:IsA("Frame") then
frame.CLAIM.Active = true
end
end
end
return DailyRewardManager
Local Script :
local TimeRemaining = script.Parent
local TimeRemainingEvent = game.ReplicatedStorage.RewardEvent.TimeRemaining
local RemainingSeconds = 0
TimeRemainingEvent.OnClientEvent:Connect(function(numberSeconds, desc, text)
for seconds = 1, numberSeconds do
RemainingSeconds = numberSeconds - seconds
text = desc .. FormatTime(RemainingSeconds)
TimeRemaining.Text = text
wait(1)
end
TimeRemaining.Changed:Connect(function()
TimeRemaining.Text = text
end)
end)
function FormatTime(seconds)
local hours = math.floor(seconds / 3600)
local minutes = math.floor((seconds % 3600) / 60)
local remainingSeconds = seconds % 60
return string.format("%02d:%02d:%02d", hours, minutes, remainingSeconds)
end