Recent datastore issue in studio

EDIT:

Datastore saving seems to be working, but for some reason it doesnt work in studio when you leave play mode. The code below shows datastore saving and loading in the same game session without leaving.

DataStoreService = game:GetService("DataStoreService")
HttpService = game:GetService("HttpService") 
DataStore = DataStoreService:GetDataStore("Test1")

game.Players.PlayerAdded:Connect(function(player)
	local key = player.userId
	
	
	
	local SaveTable = {}
	local key = player.userId
	SaveTable["NOOB"] = true
	local EncodedSavedValues = HttpService:JSONEncode(SaveTable)	

	
	local suc,err = pcall(function()
		DataStore:UpdateAsync(key,function(oldData)
		    return HttpService:JSONEncode(SaveTable)
		end)
	end)
	
	
	local EncodedData
	local suc,err = pcall(function()
		EncodedData = DataStore:GetAsync(key)
	end)
	
	print(EncodedData)

	if suc and EncodedData and HttpService:JSONDecode(EncodedData) then
		local Data = HttpService:JSONDecode(EncodedData)
		print(Data["NOOB"]) 
	end
	
end)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Saving datastore in studio to be able to debug easier.

  1. What is the issue? Include screenshots / videos if possible!

This issue didnt happen as far as I know since 2018. Roblox did alot of updates and options were moved all over the place. I made sure to enable studio api and datastore, but nothing seemed to work.
TL;DR Datastore saves fine ingame but it wont save in studio.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have searched for posts several hours regarding this issue. Mostly they dont seem to be specific enough to cover this issue. I am sure my implementation should be fine.

These are the options I have for my datastore.


image

Dont be mislead by Datastore2. This is just the name of the place and not in any ways related to the module!

As I said before it works fine when I play ingame where it saves, but Ive been spending many hours trying to figure out why it doesnt save in studio.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

I tried creating the simplest datastore script just for testing purposes. My original script has a very complex array that saves.

DataStoreService = game:GetService("DataStoreService")
HttpService = game:GetService("HttpService") 
DataStore = DataStoreService:GetDataStore("Test")

game.Players.PlayerAdded:Connect(function(player)
	local key = player.userId
	local EncodedData
	local suc,err = pcall(function()
		EncodedData = DataStore:GetAsync(key)  -- always returns nil
	end)	
	if suc and EncodedData and HttpService:JSONDecode(EncodedData) then
		local Data = HttpService:JSONDecode(EncodedData)
		print(Data["NOOB"]) 
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local SaveTable = {}
	local key = player.userId
	SaveTable["NOOB"] = true
	local EncodedSavedValues = HttpService:JSONEncode(SaveTable)	
	print(EncodedSavedValues)
	DataStore:SetAsync(key,EncodedSavedValues)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I am aware that I might have missed out on an option for turning on this for studio. I just dont understand why it should be this nested just to find this property.

1 Like

Sorry if i didn’t read it all but. Studio isn’t reliable enough to save Data. Sometimes it saves but 9/10 it doesn’t save. Unfortunately.
The script you have should work perfectly fine in the actual game.

1 Like

I had a similar issue to this recently, but was using the normal DataStores. My script ended up performing correctly after I set the game to Public, maybe try this?

1 Like

Thanks for answering. It solved my problem in the first part, but the datastore became incosistent and stopped saving. Seems like there is a problem with saving when leaving player mode. It saves perfectly fine when writing and getting datastore from the same gamesession

Edit: It seems like you are right about the part with consistency. It is very inconvinient to work without having studio save with datastores. I might file a report to bugs
Yeah thanks for answering. The reason I am using datastore in studio is to debug my massive complex array when saving. It seems like the issue with saving was caused by the game not being active as pointed out by @DyIan_II.

Here is an example of a nested array that I am saving.

Its a bit odd though. It worked for a small moment, but then it broke again. Im not sure if roblox datastore is working properly

It is advised not to save data in studio, because when you finish play testing, the server closes instantly, not giving any time for data to save. This is more or less an intended feature. In the game on the website, however, data has time to save.

To stop it from saving, use this line of code:

if game:GetService("RunService"):IsStudio() then return end
1 Like

Thank you. I am aware of the IsStudio() function and uses it to load in different modules based on if i am in studio or not. Since this was very specific to datastore I limited the example code to just datastores. The only issue is that it is hard to test datastore when you have to enter a live server each time or use test mode. Test mode has its cons where getting the asset or other function of the players id is invalid due to their userid being negative.

When it comes to relying on PlayerAdded for features in games and if you have yielding functions before it connects, you should always be using a named function. You can connect and run it for all players in the game this way. With Studio, PlayerAdded will sometimes (or never) fire, so you need to handle that.

local Players = game:GetService("Players")

local function playerAdded(player)
end

local function playerRemoving(player)
end

Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerRemoving)

for _, player in ipairs(Players:GetPlayers()) do
    playerAdded(player)
end
3 Likes

Thank you. I do have it connected like this with a taglist but since this was related to datastores I decided to take it out from the example code above. Usually when loading modules the playeradded event doesnt fire.


this is a small snippet from the original code. key is just a test variable that im gonna remove.

image

So It seems like if you bindtoclose in studio playmode it will save. This will lag you for about 2-3 sec when you leave playermode, but is very useful as you can toggle this on and off whenever you wanna debug the datastore

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then
		-- Save your data
	end
end)
1 Like