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)
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.
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:
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.
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.
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)