Datastore working in Roblox Studio but fails in game

local data = {}
data.gem=100
InventoryStore:SetAsync(player.UserId, data)

strangely, this works in roblox studio across multiple sessions and even when i read the value in game.
However when i try to do the above in game, it doesn’t even save.

Tried searching around. Cant find any info.

Any one kindly enlighten me. Thanks!

2 Likes
  1. Please, use backquotes to make the script so we don’t have to read it hideously.

  2. I believe it’s not working because that’s the full script you had. Please show us the full script so we can fully understand.

  3. If that’s not the case, that means you didn’t turn on Roblox Studio’s game setting where you must turn on the API services for Roblox studio.

1 Like

When you are in the actual game, can you see any errors in the Developer Console?

2 Likes

Do you have a game:BindToClose() function, which yields the server from shutting down until the function bound is executed?

2 Likes

It doesn’t mean prevents the server from shutting down until data store is saved. It means that when the server is closing(examples: the last player leaves in the server, the server shuts down for some reason), this event will be triggered and does whatever code inside it. It’s very useful if you need to save data while the game is gonna close for updates or whatever.

3 Likes

It won’t yield till the function is completed executing, as you said, but it does wait a maximum of 30 seconds for the function bound to be completed. After that it shuts down the server regardless of it completing or not.

Can you use an example for this? I don’t understand. Also what does yield means?

I meant to say that the game will wait till that function is completed.

1 Like

First;

  • Indent your code and use the format.

The issue is with not using pcalls, saving/getting data is not done inside Studio, it’s done outside of Roblox. So you wrap your get/save functions or methods into a pcall so that if an error were to occur, it won’t break the entire script.

Do you change your values locally or on the server side? If so, changes made on the client will not replicate to the server as FilteringEnabled prevents that. Therefore, you would have to make use of Remote events.

Therefore your code should be somewhat like this:

local data = {}
data.gem = 100

local success, errormessage = pcall(function()
      InventoryStore:SetAsync(player.UserId, data)
end)

--// handling

For anyone, here is the indented code properly formatted;

local data = {}
data.gem = 100

InventoryStore:SetAsync(player.UserId, data)

How’s he supposed to indent 3 lines with no scoping? It’s really not that big a deal, and the poor formatting is still readable too (it’s just 3 lines).


Also, you should directly call the function you’re pcalling instead of wrapping it in an anonymous function.

As in, do not do it like this:

But instead like this:

local success, err = pcall(InventoryStore.SetAsync, InventoryStore, player.UserId, data)
4 Likes

Well, you are creating a function every time that is running. I would recommend reading this topic on pcalls.

1 Like

No need, I understand them now.