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)