Weird server/client side bug

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
2 Likes

i think it might have to do with the os.time() function, because it is based on the timezones of the server and the player
I had this same issue and fixed it by using this code instead:

DateTime.now().UnixTimestampMillis / 1000

You could also make a function which is much easier to use like this:

local function getTime()
	return DateTime.now().UnixTimestampMillis / 1000
end

lmk if it fixed it

1 Like

It doesn’t matter what timezone the server is in.

2 Likes

Sort of right?
Roblox UPDATE YOUR DOCUMENTATION!

Somehow people are still reporting this error even with the switch to workspace:GetServerTimeNow()???
w h a t

I got the docs updated with this info.

https://create.roblox.com/docs/en-us/reference/engine/datatypes/DateTime#now
https://create.roblox.com/docs/en-us/reference/engine/libraries/os#time

1 Like