Data Store Not Working

You dont need to use return dataToSave, but yes, try that, as long you updated the dataToSave

The Above Script I used Gives me the Error

image

Can you print out the key and dataToSave?

1 Like

image

Try updating the dataToSave? Seems like you arent actually setting it to anything atm.

1 Like

So I mean Just before the Line i Print the Data To Save i Update it? ( Sorry If I Say Something Which Does not Make Sense Im Not Good At This :frowning_face: )

Just update the dataToSave table before you try updating the data store with it, even though Im not 100% sure if thats even the error youre getting.

The second parameter of DataStore:UpdateAsync() is a transform function, not the data.


@TigerLeo77 @YT_Forceman You can use pcalls like pcall(dataStore.GetAsync, dataStore, key).

uh So what Should i do next? Kinda Stuck

My bad, thanks for the info, I mainly use datastore2, so I forgot some of the syntaxes

Can you please provide your current script?

1 Like
local ds = game:GetService("DataStoreService")
local dataStore = ds:GetDataStore("OldTasks")

game.Players.PlayerAdded:Connect(function(player)
	local lead = Instance.new("Folder")
	lead.Name = "leaderstats"
	lead.Parent = player

	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = lead
	Cash.Value = 10

	local Old = Instance.new("Folder")
	Old.Parent = player
	Old.Name = "OldTasks"

	local key = player.UserId

	local success, data = pcall(dataStore.GetAsync, dataStore, key)

	if success and data then
		--There is some data
		print(player.Name.." Has Some Data")
		for i, OldData in pairs(data) do
			print("Old Data Loading For ".. player.Name)
			local n = Instance.new('StringValue')
			n.Name = OldData
			n.Parent = player.OldTasks
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	--Saving data
	local dataToSave = {}
	local key = player.UserId

	for i,v in pairs(player.OldTasks:GetChildren()) do
		table.insert(dataToSave, v.Name)
	end

	--Save the table to a key
	local success, errorMessage = pcall(function()
		print(key)
		print(dataToSave)
		dataStore:UpdateAsync(key, dataToSave)
		return dataToSave
	end)
	if success then
		print("Successfully saved data!!!")
	else
		print("ERROR WHILE SAVING DATA: ", errorMessage)
	end
end)

No, when you provide more then one argument to pcall, it calls the first as a function and passes the second+ as arguments to that function. What he did was actually the better/more efficient way.

The reason its like that is because you called a “:” function with a “.”, so the first argument of the function always has to be “self”, which in this case is datastore.

Alright, you are passing the second parameter of DataStore:UpdateAsync() as the data, not a transform function.

I recommend looking at this article on Data Stores, specifically the Updating Data section.

@YT_Forceman , are you sure that the data got saved before studio closed? I mean when you press stop playing then the event Players.PlayerRemoving gets fired so then the data gets saved. But then as studio closes, is there any time left to save the data? I think this is the issue. I might not be sure. Also the script that I sent worked perfectly with me.

How would i know if the data got saved before studio closed?

If Data saved successfully is printed then its 100% sure that data got saved. I recommend you to make another script which saves the data of each player every 30 seconds or so and make it print Successfully saved data after saving. Also, when u click on the stop playing button, then sometimes studio takes a while to close which means it might’ve saved the data. Try this.

Yea… When i leave it does not print data saved

This means studio is closing before data is saving. No time left to save the data.

So what do i do next? Make a script which saves data every few seconds?