I am having issues when retrieving the most recent data from the server through RemoteFunctions because of the latency issues regarding them, by the time it makes the roundtrip back the data could already be outdated.
I wrote a bit of code below to give an example of when initializing data through a remotefunction could already be outdated by the time it makes it trip back to the client.
Server
-- Localize Globals
local task_wait = task.wait
local task_defer = task.defer
-- Services
local ReplicatedStorage = game:GetService 'ReplicatedStorage'
local Players = game:GetService 'Players'
-- Events
local OnDataUpdated = ReplicatedStorage:WaitForChild 'OnDataUpdated'
local RequestData = ReplicatedStorage:WaitForChild 'RequestData'
-- Information
local Num = 0
-- Functions
local function onDataRequested()
return Num
end
task_defer(function()
while true do
Num += 1
OnDataUpdated:FireAllClients(Num, 'Update')
task_wait()
end
end)
-- Connections
RequestData.OnServerInvoke = onDataRequested
Client
-- Services
local ReplicatedStorage = game:GetService 'ReplicatedStorage'
-- Events
local OnDataUpdated = ReplicatedStorage:WaitForChild 'OnDataUpdated'
local RequestData = ReplicatedStorage:WaitForChild 'RequestData'
-- Information
local CurrentNum = -1
-- Functions
local function UpdateData(num, invokedBy)
local result
if (num > CurrentNum) then
CurrentNum = num
result = 'Updated'
elseif (num == CurrentNum) then
result = 'Up to date'
else
result = 'Outdated'
end
print(('%s [%s] (Invoked by: %s)'):format(num, result, invokedBy))
end
-- Connections
OnDataUpdated.OnClientEvent:Connect(UpdateData)
UpdateData(RequestData:InvokeServer(), 'Request')
I’m aware that I can’t doing anything to reduce the latency, but I am looking for workarounds so that old data doesn’t overwrite the most recent data.
I have already thought up a couple solutions to prevent this
Used a RemoteEvent to let the client know a update has occured then retrieve the data through a RemoteFunction this way all data is retrieved in the order they were called.
Timestamp all data sent and compare it to the current data (even though this could go wrong because multiple data can be changed on the same timeframe)
Similar to step #2 use a incremented id and compare it to the current data
I made this post entirely to see how people would work around this and possibly give a more elegant solution than what I have thought of.
They do, but my worry is when a player joins the server and gets initialization data by the time it makes the trip back it could potentially be outdated.
As you can see in the output section of my post the data was already updated to 7 before the request(5) made it back to the client.
I think you’re misunderstanding the problem, I’ll try to give a example.
Let’s say when a player joins a server they want to retrieve their inventory info from the server, we would a invoke a remotefunction that returns that data back to the client. BUT if that players inventory was to be updated with new information (via RemoteEvent) before that remotefunction made its trip back to the client. That new information would reach the client first before the information we requested would.
Meaning the players inventory will display the data from the remoteevent first then it would be overriden with the data from the remotefunction, leaving the information being displayed outdated.
You would use one or the other, either an Event or Function, not both. For getting the inventory you should choose one based on whether or not something other than the client can change the inventory. The server-to-client remote gives the player their updated inventory, there can be a separate client-to-server event that requests a manual update. If these use the same RemoteEvent instance, then order is enforced and your out of order condition never happens.