So I am creating a DataStore system for my Adventure game. I've noticed that when I complete a chapter, then rejoin, it doesn't save/load. Anyone have any answers for this?
Here is my code.
local DatastoreService = game:GetService("DataStoreService")
local myDatastore = DatastoreService:GetDataStore("CRDreamDStore")
game.Players.PlayerAdded:Connect(function(player)
local ChapterFolder = Instance.new("Folder")
local Chapter1 = Instance.new("BoolValue")
local Chapter2 = Instance.new("BoolValue")
ChapterFolder.Name = "ChaptersUnlocked"
ChapterFolder.Parent = player
Chapter1.Name = "Chapter_1"
Chapter1.Parent = player.ChaptersUnlocked
Chapter1.Value = true
Chapter2.Name = "Chapter_2"
Chapter2.Parent = player.ChaptersUnlocked
local data
local success, errormessage = pcall(function()
data = myDatastore:GetAsync(player.UserId.."-CRChapterSave")
end)
if success then
Chapter1.Value = data
Chapter2.Value = data
print("Loading Data Success")
else
print("There was an error loading Data.")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDatastore:SetAsync(player.UserId.."-CRChapterSave",player.ChaptersUnlocked.Chapter_1.Value)
myDatastore:SetAsync(player.UserId.."-CRChapterSave",player.ChaptersUnlocked.Chapter_2.Value)
end)
if success then
print("Data Successfully saved")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
Anything helps!

2 comments
I would recommend you use tables to save data. Right now, you are making 2 :SetAsync() but when you use table, then you only use 1 setasync and you save the table. Check my answer.
You could use a different key for each chapter save