Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Disconnecting the OnUpdate event?

Asked by 8 years ago

I fully understand how to use data store, and how UpdateAsync works. HOWEVER, when it comes to the wiki, it's explanation on why theOnUpdate event should be disconnected is completely obfuscated to me.

What do they mean by: "It is also advised that you disconnect OnUpdate events when you no longer need them to free up the number of times it can be fire without getting throttled"

Does this mean if I have one OnUpdate event in my game, and it's fired several times that it'll cause something to go wrong? Why is it "advised" to disconnect it? And what if a part of your game depends on using it, would it still be recommended to disconnect it?

If anyone could answer any of these questions, I'd be more than grateful. This is something I'm definitely going to be using, and would really help if I knew how to use it correctly.

Quote source: http://wiki.roblox.com/index.php?title=Data_store#Limitations

1 answer

Log in to vote
2
Answered by
Merely 2122 Moderation Voter Community Moderator
8 years ago

What happens behind the scenes is that the game server sends request to the Data Store every X seconds to check if the key has changed.

For example, if you connect OnUpdate for a key for each specific player when they join a game

local connection = DataStore:OnUpdate(player.userId .. "/stats", function(value)
    print("the key was changed to " .. value)
end)

If the player leaves, you need to manually make sure you disconnect that event.

player.PlayerRemoving:connect(function()
    connection:disconnect()
end)

This tells the game server that you no longer care about updates to that key. Otherwise, you would have a constantly increasing list of keys that you are waiting for updates on, and the game server might reach the limit of keys that it can listen to.

Ad

Answer this question