DataStore wont actually save and I don't know why. I tried problem solving

Hey, I’ll make it quick and simple. I don’t know if my DataStore is actually working. The script fires normally without any issue, no warnings or fails. But It doesn’t save my data. I don’t even know if it would load it either; but that’ll be a different post if it has to be.

Below is part of the script that handles when the player leaves and the save data should save. I can also add when the player is added. For privacy reasons I can’t show the whole script but I can show as much as I can :smiley:

Any help or insight is appreciated :smiley:

...

local success = nil
	local playerData = nil
	local attempt = 1
	
	repeat
		success, playerData = pcall(function() 
			return database:GetAsync(player.UserId)
			
		end)

		attempt += 1
		if not success then
			warn(playerData)
			task.wait(3)
		end
	until success or attempt == 5
	
	if success then
		print("Connected Successfully. Saving...")
		if not playerData then
			print("Failed to Connect to Roblox DataStore and Save Data Services. Contact Banoi.")
			
			playerData = {
				
				["Cash"] = 500,
				["Nails"] = 0,
				["Wire"] = 0,
				["Electronics"] = 0,
				["Cellphone"] = 0,
				["Soap"] = 0,
				
				
				["Empty"] = 0
			}
		end
		
		sessionData[player.UserId] = playerData
		
	else
		
		warn("Unable to fetch player data for;",player.UserId," - We are sorry. Please contact Banoi.")
		player:Kick("We are unable to load your data. Please reconnect or contact Banoi. We are sorry for this inconvenience!")
		
	end
	
	wait(1)
	-----------------------
	
	leaderstats.Parent = player
	if ResetStatsFunct == true then
		print("Stats are Resetting; reset is true. True either by Manual Activation or Server Error")
		wait(5)
		item_Nails.Value = 0
		item_Wire.Value = 0
		item_ElectricScrap.Value = 0
		item_Cellphone.Value = 0
		item_Soap.Value = 0
		
		ResetStatsFunct = false
	else
		print("Stats are safe; reset is false.")
	end
end

Players.PlayerAdded:Connect(PlayerAdded)

function PlayerLeaving(player)
	if sessionData[player.UserId] then
		local success = nil
		local errMSG = nil
		local attempt = 1
		
	repeat
		success, errMSG = pcall(function() 
				database:SetAsync(player.UserId, sessionData[player.UserId])
		end)
		
		attempt += 1
		if not success then
			warn(errMSG)
			task.wait(3)
		end
	until success or attempt == 5
	
		if success then
			print("Data stored under the User: ", player.Name, " has been aquirred.")
		else
			warn("Unable to save for the User: ", player.Name)
		end
	end
	
	print(player,"Left the game. See you soon!")
	task.wait(1)
end -- This end is connected to when the player enters the game. Its not loose.

Players.PlayerRemoving:Connect(PlayerLeaving)

can you show the part where it gets or uses the data

also try to print the data when it loads in

I mean are there any prints outputting in your PlayerAdded event? It can be a basic mistake that the players are already in the game before the PlayerAdded even connects, in this case, use this pattern:

local function PlayerAdded(player)
    -- code
end

Players.PlayerAdded:Connect(PlayerAdded)
for _, player in Players:GetPlayers() do
    task.spawn(PlayerAdded, player)
end

Second, I’m fairly sure you have to convert player.UserId into a string (tostring(player.UserId)) when using Get, Set, etc.

I assumed it was a few lines down; repeat success, playerData = pcall(function() etc.

I’m trying to make a savable inventory system and I’m sure my code is a mess. All my print’s fire in their correct path. It says it saves. Doesn’t warn or say its failed. But for some reason it still fails all together?

Adjusted it, I do get an error saying Argument 2 missing or nil. So clearly I got some issues to deal with. I’ve never come across this error before, do you know how to solve it? I get that warning from

        if not success then
			warn(errMSG)
			task.wait(3)
		end

in the ‘PlayerLeaving’ function.

That’s not it. I meant the first argument - your table uses the UserId number as actual indices.

-- example
database:GetAsync(tostring(player.UserId))
sessionData[player.UserId] = ...

database:SetAsync(tostring(player.UserId), sessionData[player.UserId])