Data Store Not Working

You See, I Have a Folder Inside the Player Called “OldTasks”.
I add String Values Inside of the Folder Accordingly.

Iv Tried Making A DataStore for Storing the Folder’s Children So that when the Player Joins Back again, The Folder Gets Back its Old Children.
It Apparently Does not Work.

The Problem (I Think) Comes while Loading the Data Back
Please Check if I made any Errors!

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"
	
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
dataStore:UpdateAsync(key, function(oldValue)
		print(key)
		return dataToSave
	end)
end)

game.Players.PlayerAdded:Connect(function(player)

	local key = player.UserId
	local loadedData = dataStore:GetAsync(key) --Example
	if loadedData ~= nil then
		print(player.Name.." Has Some Data")
		wait(0.2)
	for i, OldData in pairs(loadedData) do
			print("Old Data Loading For ".. player.Name)
			local n = Instance.new('Folder')
			n.Name = OldData
			n.Parent = player.OldTasks
	   end
	end
end)
2 Likes

Does it print, “YT_Forceman has some data”?

Yes It Prints “YT_Forceman Has Some Data”

Any errors in the output? What exactly is the problem, does anything load in at all?

Its Supposed to Load the Children As A String Value to the Folder When the Player Joins Again.
It Does not do that. It Just Prints that I Have data and Nothing Else Ahead of That.

Output Has no Errors.

Is the data you’re trying a save a table?

Your script has no errors in it. But, you have made a new ‘Folder’ instead of ‘StringValue’ here:

for i, OldData in pairs(loadedData) do
	print("Old Data Loading For ".. player.Name)
	local n = Instance.new('Folder') --This should be local n = Instance.new('StringValue')
	n.Name = OldData
	n.Parent = player.OldTasks
end

Also, I recommend you to use pcall() so that we can keep a track of the errors. Here is a better way you can do the same:

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(dataStore.UpdateAsync, dataStore, key, function()
		print(key)
		return dataToSave
	end)
	if success then
		print("Successfully saved data!!!")
	else
		print("ERROR WHILE SAVING DATA: ", errorMessage)
	end
end)

I think this is what you want:
image
Here I have saved a,b,c in the datastore. So, 3 string values are made whose names are set to a,b,c respectively.

1 Like

Yes Exactly! Thats what i Want!

Once i Tried the Script You Gave me It Does not Even Save the Data (It Does not Print "Data Saved Sucessfully!!!)

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

Should be

local success, data = pcall(function()
   dataStore:GetAsync(dataStore, key)
end)
1 Like

Oh Yeah i Guess he made a mistake?

--Save the table to a key
	local success, errorMessage = pcall(dataStore.UpdateAsync, dataStore, key, function()
		print(key)
		return dataToSave
	end)

This is also wrong and should be

--Save the table to a key
	local success, errorMessage = pcall(function()
		print(key)
        dataStore:UpdateAsync(dataStore, key)
		return dataToSave
	end)

image

What Does Unable to Cast Value to function Mean?

image

image

Replace dataStore:UpdateAsync(dataStore, key) with dataStore:UpdateAsync(key). The first argument is the player, meaning its trying to cast the data store to a player, which is impossible.

1 Like

image

You arent updating the data store with anything either, so put some value after the player value (key, valueToUpdate)

1 Like

Probably dataToSave Im assuming?

1 Like
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)
		dataStore:UpdateAsync(key, dataToSave)
		return dataToSave
	end)
	if success then
		print("Successfully saved data!!!")
	else
		print("ERROR WHILE SAVING DATA: ", errorMessage)
	end
end)

Like this?