DataStore not saving or loading data but the other DataStore did

I made a DataStore in my game but it won’t save or load its data unlike the other DataStore that I’ve made. Here is the source code of the script that manages the DataStore that doesn’t work. By the way the class of the script is ServerScript and is parented to ServerScriptService

local DataStore = game:GetService("DataStoreService"):GetDataStore("UnlockedLands")

game.Players.PlayerAdded:Connect(function(Player)	
	local UnlockedLands = Instance.new("Folder",Player)
	UnlockedLands.Name = "UnlockedLands"
	
	local Data
	local Success
	local ErrorMessage
	local Attempts = 0
	
	repeat
		Attempts = Attempts + 1
		Success,ErrorMessage = pcall(function()
			Data = DataStore:GetAsync(Player.UserId.."-UnlockedLands")
		end)
		
		if ErrorMessage then wait(1) end
		
	until Attempts == 5 or Success
	
	game.ReplicatedStorage.DataStoreLoaded:FireClient(Player)
	
	if Success then
		print("Succefully loaded "..Player.Name.."'s history of unlocked lands!")
		if Data == nil then
			local StarterLand = Instance.new("BoolValue",UnlockedLands)
			StarterLand.Name = "Forest"			
		else
			for i = 1,#Data do
				local BoolValue = Instance.new("BoolValue",UnlockedLands)
				BoolValue.Name = Data[i]
			end
			
		end
	else
		warn(ErrorMessage)
		print("An error occured while loading player's data!")
		Player:Kick("Your data didn't load properly. Check your internet connection and try again!")
	end
	
	repeat wait() until not Player:WaitForChild("PlayerGui"):FindFirstChild("LoadingScreen")
	local Audio = game.ServerStorage.Audios.Forest:Clone()
	Audio.Parent = Player:WaitForChild("PlayerGui") -- PlayerGui can play audios so the parent is player gui
	Audio:Play()
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local ToBeSaved = {}
	
	for i,v in pairs(Player:WaitForChild("-UnlockedLands"):GetChildren()) do
		table.insert(ToBeSaved,v.Name)
	end
	
	local Success
	local ErrorMessage
	local Attempts = 0
	
	repeat
		Attempts = Attempts + 1
		Success,ErrorMessage = pcall(function()
			DataStore:SetAsync(Player.UserId.."-UnlockedLands",ToBeSaved)
		end)
		if ErrorMessage then wait(1) end
		
	until Attempts == 5 or Success
	
	if ErrorMessage then
		warn(ErrorMessage)
		print("An error occured while saving player's data")
	end
	
end)
1 Like

I’ve found these two videos by @.Alvin_Blox and knowing that they’re probably more in depth and will help you fix your problem(s).

2 Likes

You are creating the folder “UnlockedLands” but when the player leaves you are searching for the folder “-UnlockedLands”, 2 different strings

2 Likes

Check if you have enabled Studio Access to API Services, if you haven’t then enable it.
And also next time try and send a pic of output window, it really helps people who would like to help you as it is easier to find the error.

1 Like

using repeat wait() until for these cases is known as polling, using wait() is a sign of a code smell.

Your code could unnecessarily even wait after PlayerGui exists. Use some other function to detect whether LoadingScreen is nil.

1 Like

oop I didn’t notice that the script was searching for a folder named “-UnlockedLands” instead of “UnlockedLands”! btw thankss!

It sends no error at all and I guaranteed that it is super very very very rare for the DataStore to not save or load because I added a repeat - until statement for it to retry 5 times if it fails in the first attempt