I’m working on a game that uses Data stores as packets. I found something weird, the OnUpdate it not being called when I use UpdateAsync on Other server. It does get called on the server it fires on though.
This completely breaks my whole system, if this could be fixed fast I would be very grateful.
Are you sure its not being called? I’m pretty sure if you make an update from another server there is a 0-2 minute delay on other servers picking up the change and the event firing.
Can’t remember were I found this, but I do remember somewhere saying that you couldn’t really make a Chat system across servers since there would be a delay of the message getting sent to other servers. Also I think the delay is more like 0-60 seconds.
Problem is, the server doesn’t get the new data as soon as it updates. It has its own cache of the data which updates periodically (the maximum ‘delay’ time you’re experiencing). Would be nice if it were instant, but I can definitely see the change being hard to implement.
The keyword here is ‘when’. If the Servers have a delay, then it should be as less as possible. Ofcourse they can’t make it better if they haven’t a solution for the delay.
I don’t believe it was ever guaranteed to be instant. You probably should have done some preliminary testing before you went and worked on your system. It’s not really ROBLOX’s fault that it doesn’t work.
DataStores is not designed to be an instantaneous communication device between games and servers. Your updates will only be instant on the server they were called from, but each place updates it’s cache every half a minute or so.
Lesson to learn from this: Do not code for 15 hours expecting a behavior you haven’t tested yourself, or at least had documentation guaranteeing it.
When UpdateAsync is used it fires the callback function at least once, sometimes more than once (I’ll get deeper into this later). It is run in the same environment (and what I would assume to be the same thread) as the script that calls it, and no other. This means that other servers do not have the callback being fired. However, there’s something special about UpdateAsync; it updates according to the current value of the key its being called on, not the cached one (well, eventually it does). This brings us back to your callback being called more than once. The final time it is run it will be given the true value (passed through the first parameter of the given callback) to work with. Because it still runs in the same environment (and since it yields) you can set your variables accordingly. In other words:
function getRealValue(key, dataStore)
local val
dataStore:UpdateAsync(key, function(oldValue)
val = oldValue
return nil -- Cancel the key update
end)
return val
end
Oh, and don’t forget to use UpdateAsync whenever updating keys!
TL;DR
Unless I’m an idiot who doesn’t fully understand this, just use the code I posted. When I tested it it appeared to work for me, so it should work for you too.
Just adding a teeny tiny bump (sorry)… I confused UpdateAsync with OnUpdate and I don’t want anybody who saw my post before I edited it running around trying to use OnUpdate to update values.