I have been trying to fix server lag on my game, I did some tracing and came to the result of the :GetAsync call is actually causing the server to skip about 14 heartbeats/frames, to test this fully I disabled every script on the Server except for my script which detects Server Lag, and my Datastore Script, which still shows the same result. this was tested on the ROBLOX game, not in studio.
I’m unsure if this is just a me issue which I dont think it is, I’ve asked 3 other developers who have also looked into it and believe its the same issue, the size of the Datastores are only about 1,300 characters
GetData function (some stuff wont run because of the testing I’ve been doing to this function, ignore the random bits that dont run)
each time a player joins, the server skips about 11 Heartbeats/Frames
function module:GetData(User)
if typeof(User) == 'Instance' then
User = User.UserId
end
print("STRACEBACK", debug.traceback("PlayerData:GetData"))
local data
if true then
local start = os.clock()
local C = DataStores:GetAsync(User)
local Finish = os.clock()
print("PlayerData GetAsync", Finish-start)
if C then
data = httpService:JSONDecode(C)
end
return true,data
end
local start2 = os.clock()
local data
--local success,err = pcall(function()
local start = os.clock()
local Temp = DataStores:GetAsync(User)
print("PlayerData GetAsync", os.clock()-start)
--warn('PlayerData: getting data at index', User, typeof(User), ':', Temp)
if Temp then
data = httpService:JSONDecode(Temp)
local totalChar = 0
for i,v in data do
local t = string.len(httpService:JSONEncode(v))
warn(i,t)
totalChar += t
end
print(totalChar)
if tonumber(User) then
--data = fixData(data,User)
end
end
--end)
print("PlayerData:GetData", os.clock() - start2)
if true then
return true, data
end
if not success then
warn('PlayerData ran into a problem while Getting Data from ' .. User .. ' error: ' .. err)
return success,nil
else
return true,data
end
end
Core Datastore call:
function module:LoadData(User:Player)
local Start = tick()
local LastData = {}
local Data
local Success
local NewPlayer
local C1 = tick()
local succ,err = pcall(function()
Success,Data = PData:GetData(User)
end)
if true then return end
Lag Detector
local Data = {
CurrentTick = 0,
lastTickTime = tick(),
started = false
}
local function nextGameTick()
local StartTime = tick()
local ExpectTimeDifference = 1/60
local Difference = ExpectTimeDifference + (ExpectTimeDifference * 0) -- adds a minor gap to allow for simple processing on the Server
if StartTime - Data.lastTickTime >= Difference and Data.started then
local LagAmount = (StartTime - Data.lastTickTime)/Difference
if LagAmount >= 2 then
warn('Server Lag Detected, Skipped ' .. math.floor(LagAmount) .. ' heartbeats')
local LagTime = StartTime - Data.lastTickTime
warn('LagTime: ' .. LagTime)
end
end
if not Data.started then
Data.started = true
end
Data.CurrentTick += 1
Data.lastTickTime = tick()
end
game:GetService('RunService').Heartbeat:Connect(function()
nextGameTick()
end)