PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

OnUpdate

This function sets callback as the function to be run any time the value associated with the GlobalDataStore|data store's key changes. Once every minute, OnUpdate polls for changes by other servers. Changes made on the same server will run the function immediately. In other words, functions like GlobalDataStore/IncrementAsync|IncrementAsync(), GlobalDataStore/SetAsync|SetAsync(), and GlobalDataStore/UpdateAsync|UpdateAsync() change the key’s value in the data store and will cause the function to run.

See the Articles/Data store|Data Stores article for an in-depth guide on data structure, management, error handling, etc.

Parameters

Name Type Default Description

key

The key identifying the entry being retrieved from the data store

The function to be executed any time the value associated with key is changed

Returns

Return Type Summary

The connection to the key being tracked for updates


Code Samples


Print Data Store Value on Update

The sample creates an GlobalDataStore/OnUpdate|OnUpdate() connection with the key myKey and the printOut() function, then it sets the myKey store to 11. Since the connection with myKey is open, printOut() executes and prints the input (the updated value). Immediately after this occurs, the script disconnects the connection.

local sampleDataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")

local connection

local function printOut(input)
	print(input)
	-- Disconnect connection
	connection:Disconnect()
end
 
connection = sampleDataStore:OnUpdate("myKey", printOut)

local success, err = pcall(function()
	sampleDataStore:SetAsync("myKey", 11)
end)