Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

DataStores wont load. I know there is something wrong, but I can't get my finger on it. Any answers?

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

0

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.

0

You could use a different key for each chapter save

1 answer

✓ Accepted answer

This is how I recommend doing:

local DatastoreService = game:GetService("DataStoreService")

local myDatastore = DatastoreService:GetDataStore("CRDreamDStore")

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

    local data 
    local success, errormessage = pcall(function()
        data = myDatastore:GetAsync(player.UserId.."-CRChapterSave")
    end)
    if success then
        Chapter1.Value = data.Chapter1 -- I changed this a little here and changed the player removing part.
        Chapter2.Value = data.Chapter2
        print("Loading Data Success")
    else
        print("There was an error loading Data.")
        warn(errormessage)
    end
end)

And now for the player removing part:

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
	-- We make a table to store our data.
	local data = {
		Chapter1 = player.ChaptersUnlocked.Chapter_1.Value;
		Chapter2 = player.ChaptersUnlocked.Chapter_2.Value;
	}

	myDatastore:SetAsync(player.UserId, data) -- We only run SetAsync() once and save the table.

   end)

    if success then
        print("Data Successfully saved")
    else
        print("There was an error when saving data")
        warn(errormessage)
    end

end)

If you have any questions regarding this, feel free to ask below.

Hope it helps.

This discussion is preserved as it was. New answers, comments, and votes are closed.