Data save probelm

Hi there, for some reasson the data saving doesn’t work like its suppose to, it only saves sometimes and when it doesn’t save it doesn’t give me an error message or anything, the output stays blank and i dont know what i did wrong.

Heres the data save script:

function LevelSave(Player)
	local success = nil
	local errormessage = nil
	local attempts = 1
	
	repeat
		success, errormessage = pcall(function()
			MDS:SetAsync(Player.UserId.."-Level", Player.leaderstats.Level.Value)
		end)
		
		attempts += 1
		if not success then
			warn(errormessage)
			task.wait(1)
		end
	until success or attempts >= 5
	
	if success then
		print("< LevelData successfully saved >")
	else
		print("< LevelData failed to save >")
		warn(errormessage)
	end
end

Heres the full script:

local DSS = game:GetService("DataStoreService")
local MDS = DSS:GetDataStore("MDS")

local CheckXpEvent = game.ReplicatedStorage:WaitForChild("Events").RemoteEvents.CheckXp

function LevelData(Player)

	--//Create Level Values\\--
	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"
	
	Level = Instance.new("IntValue", leaderstats)
	Level.Name = "Level"
	Level.Value = 1
	
	Xp = Instance.new("IntValue", Player)
	Xp.Name = "Xp"
	Xp.Value = 0
	
	reqXp = Instance.new("IntValue", Player)
	reqXp.Name = "reqXp"
	reqXp.Value = Level.Value * 200
	
	--//Data\\--
	
	local data
	
	local success, errormessage = pcall(function()
		data = MDS:GetAsync(Player.UserId.."-Level")
	end)
	
	if success then
		print("< LevelData successfully loaded >")
		Level.Value = data
	else
		print("< LevelData failed to load >")
		warn(data)
	end
end

function LevelSave(Player)
	local success = nil
	local errormessage = nil
	local attempts = 1
	
	repeat
		success, errormessage = pcall(function()
			MDS:SetAsync(Player.UserId.."-Level", Player.leaderstats.Level.Value)
		end)
		
		attempts += 1
		if not success then
			warn(errormessage)
			task.wait(1)
		end
	until success or attempts >= 5
	
	if success then
		print("< LevelData successfully saved >")
	else
		print("< LevelData failed to save >")
		warn(errormessage)
	end
end

And yes, i am calling the functions in an game.Players.PlayerAdded:Connect(LevelData) and game.Players.PlayerRemoving:Connect(LevelSave)

1 Like

the reason why it shows no error while saving is because you are using pcall (i believe i am not sure because i only use external datastores)

use this code from the api, it never hurts to check the api

for i = 1, 5 do
	local key = "key" .. i
	local success, err = pcall(function()
		globalStore:SetAsync(key, true)
	end)
	if success then
		printBudget()
	else
		print(err)
	end
end

I solved a similar issue a while back, go check out this topic:

I know its not because of the pcall since im on my alt rn doing this trying to replicate a similar data saving script which used pcall in it aswell.