I want to be able to make this load and save but it’s not working.
It says it saves but when I join a game it will not load and I don’t understand why, I get no errors.
local GetDataStore = game:GetService("DataStoreService")
local DataStore = GetDataStore:GetDataStore("ClientData")
local NewData = {}
game.Players.PlayerAdded:Connect(function(Player)
local PlayerDataFolder = game:GetService("ServerStorage").PlayerData:Clone()
PlayerDataFolder.Parent = Player
local Id = Player.UserId
local Data
local success,errormessage = pcall(function()
Data = DataStore:GetAsync(Id)
end)
if success then
print("success")
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Id = Player.UserId
for _,v in pairs(Player.PlayerData:GetChildren()) do
NewData[v.Name] = v.Value
end
local success,errormessage = pcall(function()
DataStore:SetAsync(Id, NewData)
end)
if success then
print(NewData.Money.." - "..NewData.Level)
end
end)
have you tried using a datastore editor plugin to confirm the data has been stored?
I use DataVoid by VoidedBlade. You simply enter the datastore name and the players key and select the GetData tab and see the result in a data table.
You’ve forgotten to do anything with the data after you’ve got it from the data store!
All you need to do is loop through all the data, updating the values in the player’s data folder like so:
game.Players.PlayerAdded:Connect(function(Player)
local PlayerDataFolder = game:GetService("ServerStorage").PlayerData:Clone()
PlayerDataFolder.Parent = Player
local Id = Player.UserId
local Data
local success,errormessage = pcall(function()
Data = DataStore:GetAsync(Id)
end)
if success then
for i, v in pairs(Data) do -- Update player data with new data
PlayerDataFolderNewData[i].Value = v
end
print("success")
else
print("error: "..errormessage)
-- You may want to do something about data failing to load
-- Make sure you don't overwrite user data if it fails
end
end)
Hopefully this helps, let us know if you run into any other issues. If you want to give your user’s data the best protection from data loss but don’t want to code it yourself, try using DataStore2.
creating an extra varaible called local Data is overuse. you could simply do:
local success,response= pcall(function()
return DataStore:GetAsync(Id)
end)
if success then
for i, v in pairs(response) do -- Update player data with new data
PlayerDataFolderNewData[i].Value = v
end
print("success")
else
print("error: "..response)
end
if success is false then the response will be an error, otherwise it’s the data. this is just a way to make your code look cleaner and is less work
I did notice this when responding, but wanted to keep my response using as much of his previous code as possible. There won’t be any (noticable) impact in using the additional data. Some may even argue that the additional variable makes it easier to read; Data and errormessage would be two completely different values, whereas response isn’t exactly clear what it is, and represents different values in different places.
But yes, I completely agree with you, it’s unnecessary.