For some reason users of a game I work on are reporting negative times occurring for research that works offline.
The client invokes this function when the user clicks on a research frame
DoResearch.OnServerInvoke = function(player, research)
local Profile = ProfileService:GetProfile(player)
if Profile.Money >= Research[research].Cost and not Profile.Research[research] then
local StartTime = os.time()
Profile.Research[research] = StartTime
MainModule:AddMoney(player, -Research[Research].Gain)
return true
else
return false
end
end
Then every second the server runs this code.
for _, player in Players:GetPlayers() do
local Profile = ProfileService:GetProfile(player)
local a, b = pcall(function()
for research, StartTime in Profile.Research do
local TotalTime = Research[research].Time
if os.time()-StartTime > TotalTime or os.time()-StartTime < 0 then
Profile.Research[research] = nil
MainModule:AddMoney(player, Research[research].Gain)
end
end
end)
if b then
warn(b, " long research updater", Profile.IsLoaded) -- Error analytics only shows 26 of this warning but more than 26 users have reported it. + it would fire multiple times if it were a constant issue.
end
end
To display the remaining time client runs this every second.
local function toHMS(s)
return string.format("%02i:%02i:%02i", s/60^2, s/60%60, s%60)
end
function module:UpdateTimer() -- runs every second
local Research = ReplicatedStorage.Functions.GetProfileData:InvokeServer("Research")
for _, Frame: Frame in script.Parent.Research.Main:GetChildren() do -- Where the research frames are held.
if not Frame:IsA("Frame") then continue end
local StartTime = Research[Frame.Name]
if not StartTime then
Frame.WaitTime.Visible = false
continue
elseif os.time()-StartTime < 0 then
warn("Send in feedback this error code: ErrorCode 15 (123)", os.time(), StartTime, Research[Frame.Name].Time, Frame.Name)
elseif floor( ClientModule[Frame.Name].Time - (os.time()-StartTime)) < 0 then
-- Users have reported this error code. But it shouldn't occur because the server deals with it?
warn("Send in feedback this error code: ErrorCode 16 (125)", os.time(), StartTime, Research[Frame.Name].Time, Frame.Name)
end
Frame.WaitTime.Visible = true
Frame.WaitTime.Text = toHMS(math.floor( Research[Frame.Name].Time - (os.time()-StartTime)))
end
end