DataStore SetAsync not working

Hey! So, I am currently trying to make a datastore, that saves a table of my pets. As of yesterday evening, my script fails to SetAsync(player.userId, SaveTable). I have Studio API Access enabled and everything worked fine, until yesterday. Here’s a quick test script, that isn’t working in my game. The funny part is, that GetAsync is working, it’s just not printing the updated data.

game.Players.PlayerAdded:Connect(function(player)
	print(game:GetService("DataStoreService"):GetDataStore("Pets"):GetAsync(player.userId))
end)

game.Players.PlayerRemoving:Connect(function(player)
	game:GetService("DataStoreService"):GetDataStore("Pets"):SetAsync(player.userId, "NormalPet")
end)

Image of the output:

image

Am I doing something wrong?

Thanks in advance

-Coyann

Hi, im not the Best Scripter but u cant save tables, “NormalPet” îs a table first Make:
NNormalPet = {"NormalPet"}
NormalPet = HTTP:JsonEncode(NNormalPet)

Then în the save line instead of “NormalPet” put NormalPet

After the player joins put:

local NCode = HTTP:JsonDecode(game:GetService("DataStoreService"):GetDataStore("Pets"):GetAsync(player.UserId)

And at the print put:
``print(NCode)```

Say If u get any errors

Out of curiosity have you tried declaring a variable such as local pets = {“Pet1”} and putting “pets” inside of the second argument of SetAsync?

As far as saving tables to datastore go, I’ve never heard it not working. I use it for all of my games and I’ve not had a single problem.

I think I’ve found the problem… When I Put

game.Players.PlayerAdded:Connect(function(player)
    game:GetService("DataStoreService"):GetDataStore("Pets"):SetAsync(player.userId, "TESTPET")
	print(game:GetService("DataStoreService"):GetDataStore("Pets"):GetAsync(player.userId))
end)

It prints “TESTPET”. So… something must be preventing the PlayerRemoving function from saving it to the DataStore…:thinking:

From the code that you have proved the only thing I think could be causing SetAsync not to work is that your key isn’t a string:


This is screenshot taken from the SetAsync() page on the Developers hub and it says that the key needs to be a string. You can do this easily by doing: tostring(player.UserId). You will also need to make the GetAsync() key a string as well.

I’ve tried that too. Still doesn’t work. I’m currently saving Data every 30ish seconds as a workaround…

The API page is weird because the key is automatically coerced into a string internally (I assume through tostring). You’ll notice that no error is thrown for a numeric key. Probably just says string to keep developers away from passing arbitrary datatypes as the DataStore key.

2 Likes

Oh and surprisingly, it’s working in-game. So, this is a Studio related bug or error…

You can save tables as a DataStore value and you don’t need to JSONEncode or JSONDecode the values as it is automatically done for you.

I organized the code a little. This should work.

Pets = game:GetService("DataStoreService"):GetDataStore("Pets")

game.Players.PlayerAdded:Connect(function(player)
	local petsval = Pets:GetAsync(player.userId)
	print(petsval)
end)

game.Players.PlayerRemoving:Connect(function(player)
	Pets:SetAsync(player.userId, "NormalPet")
end)

Is Studio Access to API Services enabled?

If it was not enabled it would provide an error, but in OPs case that does not seem to be the case. It seems to return nil, meaning that the GetAsync call is working.

1 Like

I can confirm I am having the same issue. The line before the SetAsync part in my script will print, but the line after it won’t print and my data wont save. Really annoying bug.

If you create a post about it, I guarantee others will see and try to do their best to help.

I’ve already had people try to help me on Discord plus I have tested it on multiple games (including completely new places) and the same issue still occurs.

When the player leaves, the server closes before the data gets saved. You will have to use BindToClose to save the data before the server shutdowns. Check out this post that explains how to save data properly.

Still doesn’t work. Currently impossible to play test in studio atm. Thank you for the suggestion though

Yeah, that is a Roblox issue at the moment. Try it in a live game and it should work. Also you should have a loop that saves data every minute. Check out this article https://developer.roblox.com/en-us/articles/Saving-Player-Data

If you want me to look over the code, send me a message and I’ll do my best to help.