We use cookies on this site to enhance your user experience

Print Data Store Value on Update

Print Data Store Value on Update

Sep 05 2018, 8:12 PM PST

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)
	-- Disconnect connection
connection = sampleDataStore:OnUpdate("myKey", printOut)

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