Event for Datastore keys changing values?

Hi, I was wondering if there is an alternative to DataStore:OnUpdate. The problem with this, is that it needs a callback. I cannot provide that because callbacks can’t yeild, so I was wondering if there was an event I could use to detect a change in a key value. Thanks.

1 Like
connection = DataStore:OnUpdate("ChatNumber", function(number)
     local message = DataStore:GetAsync("Chat"..number)
     game.ReplicatedStorage.UCS.loadMessage:FireAllClients(message)
     connection:Disconnect()
end)

I think it’s because I’m using GetAsync, I’m getting the error “Callbacks cannot yield”

1 Like

Are you getting data or setting it? If you’ve already set data, you use UpdateAsync to update it.

1 Like

Looks like I’m wrong then
You can use spawn to call your yielding stuff in a different thread

connection = DataStore:OnUpdate("ChatNumber", function(number)
     spawn(function()
          local message = DataStore:GetAsync("Chat"..number)
          game.ReplicatedStorage.UCS.loadMessage:FireAllClients(message)
     end)
     connection:Disconnect()
end)
1 Like

Thank you.

1 Like

Quick question, how would I end a spawn yield? It looks like my code won’t run for a second time after that.

1 Like

your code doesn’t run again because of this line

connection:Disconnect()

That stops the callback from being called again
Just delete that line if you want to maintain the connection

1 Like

Still only running it once :confused:

connection = DataStore:OnUpdate("ChatNumber", function(number)
print("Number: "..number)
spawn(function()
local message = DataStore:GetAsync("Chat"..number)
print(message)
game.ReplicatedStorage.UCS.loadMessage:FireAllClients(message)
print("Sent")
end)
end)

Even without the spawn function, the OnUpdate function only runs once, must be a bug.

1 Like

Reopening the thread, because this issue is still relevant to my situation.

2 Likes

This doesn’t work, it’s currently broken.


More information about Datastore.

OnUpdate currently does not work as intended. You need to poll some kind of update yourself by containing a GetAsync in a loop that will cause OnUpdate to run.

I advise holding off on using that function altogether, as well as whatever functionality you need it for and waiting until the MessagingService goes live. The MessagingService is intended to be a service that allows you to communicate with other servers live. With this, you’ll be able to write your own update function that sends a message to other servers that a DataStore update was made.

-- For example:
local function UpdateDataStores(args)
   whack_datastore_function()
   messagingservice_send_message_thing()
end

ChatNumber is being changed by 1 every time someone chats.

DataStore:IncrementAsync("ChatNumber",1)

That’s what I had in mind but I wanted to see if there was any other way of making the code efficient.

Thank you for the info!