DataStore won't load

hello, this is a datastore that loads some cosmetics onto the player
I changed a bit my old datastore code because it was horribly optimized and after following a tutorial i was hoping this way was more optimized

currently im having issues with

local function loadData(player)

specifically

repeat task.wait()
		success, errorMessage = pcall(function()
			data = inventoryDS:GetAsync(userId)
			print(success)
		end)
		attemptsLeft -= 1
	until (attemptsLeft == 0) or success

as it gives me nil when save data gives me proper results
13:39:11.054 ▼ {
[1] = “unusual_selfmade”
} - Server
which its the item it saves but it just seems that loaddata is unable to catch it and idk why

dss = game:GetService('DataStoreService')
inventoryDS = dss:GetDataStore('inventoryDS')
playerService = game:GetService('Players')
rs = game:GetService('ReplicatedStorage')
player = game:GetService('Players')


local function serialiseData(player)

	local accessorySave = {}
	local inv = player:FindFirstChild('Inventory')

	for _,acc in pairs(inv:GetChildren()) do
		table.insert(accessorySave, acc.Name)
	end
	return accessorySave 
end

local function deserialiseData(data)

	local playerUserId = player.UserId

	local folder = rs:FindFirstChild('Sample'):Clone()
	local isSample = rs:FindFirstChild('ItemStorage')
	folder.Name = 'Folder_'..playerUserId
	folder.Parent = rs

	if folder.Name == 'Folder_'..playerUserId then

		for _,items in pairs(data) do
			isSample[items]:Clone().Parent = folder

		end

		for _, child in pairs(folder:GetChildren()) do
			child:Clone().Parent = player:WaitForChild('Inventory')
		end

	end

	return data
end

local function saveData(player)
	local data = serialiseData(player)
	local attemptsLeft = 10
	local success, errorMessage
	repeat task.wait()
		pcall(function()
			success, errorMessage = inventoryDS:UpdateAsync("UserInventory-"..player.UserId, function(old) --Save
				if not (old and old.DataId) then
					return data
				end			
			end)

		end)

		attemptsLeft -= 1
	until (attemptsLeft == 0) or (success)

	if success then 
		print(player.Name .. ' data saved')
	else
		warn(player.Name .. ' not saved', errorMessage)
	end

	return success, errorMessage
end

game:BindToClose(function()
	for _, p in ipairs(playerService:GetPlayers()) do
		saveData(p)
	end
end)

local function loadData(player)
	
	local userId = player.UserId
	local data
	local attemptsLeft = 10
	
	local success, errorMessage
	repeat task.wait()
		success, errorMessage = pcall(function()
			data = inventoryDS:GetAsync(userId)
			print(success)
		end)
		attemptsLeft -= 1
	until (attemptsLeft == 0) or success

	if (not success) then
		warn(errorMessage)
		return
	end
	print(data)
	if data then
		--//Old Player
		local folder = rs:FindFirstChild('Sample'):Clone()
		local isSample = rs:FindFirstChild('ItemStorage')
		folder.Name = 'Folder_'..player.UserId
		folder.Parent = rs

		if folder.Name == 'Folder_'..player.UserId then

			for _,items in pairs(data) do
				isSample[items]:Clone().Parent = folder
				
			end

			for _, child in pairs(folder:GetChildren()) do
				child:Clone().Parent = player:WaitForChild('Inventory')
			end

		else
			--//New Player
			return
		end
		
		return true
	end
end

local function onPlayerAdded(player)
	loadData(player)
end

local function onPlayerRemove(player)
	saveData(player)
end


local function initialise()
	for _, player in ipairs(playerService:GetPlayers()) do
		onPlayerAdded(player)
	end
end

playerService.PlayerAdded:Connect(onPlayerAdded)
playerService.PlayerRemoving:Connect(onPlayerRemove)

Is there any error in the output??

1 Like

no errors at all, just nil
data = inventoryDS:GetAsync(userId)
isn’t catching the data saved

incase somebody asks this was the old code ( which works ) but chokes the game a lot when it launches

dss = game:GetService('DataStoreService')
inventoryDS = dss:GetDataStore('inventoryDS')
player = game:GetService('Players')
rs = game:GetService('ReplicatedStorage')

game.Players.PlayerAdded:Connect(function(player)
	local playerUserId = player.UserId
	
	local data
	local success, errorMessage = pcall(function()
		data = inventoryDS:GetAsync("UserInventory-"..playerUserId) --Load
	end)

	if success then
		local folder = rs:FindFirstChild('Sample'):Clone()
		local isSample = rs:FindFirstChild('ItemStorage')
		folder.Name = 'Folder_'..playerUserId
		folder.Parent = rs
		
		if folder.Name == 'Folder_'..playerUserId then
			
			for _,items in pairs(data) do
				isSample[items]:Clone().Parent = folder
				
			end
			
			for _, child in pairs(folder:GetChildren()) do
				child:Clone().Parent = player:WaitForChild('Inventory')
			end
			--task.wait(0.5)
--			print(player:WaitForChild('Inventory'):GetChildren())
		else
			print('Player_'..playerUserId..' had no items')
		end

	else
		warn("Error: "..errorMessage)

	end	


end

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = player.UserId
	local accessorySave = {}
	local inv = player:FindFirstChild('Inventory')

	for _,acc in pairs(inv:GetChildren()) do
		table.insert(accessorySave, acc.Name)
	end
	
	
	local success, errorMessage = pcall(function()
		inventoryDS:SetAsync("UserInventory-"..player.UserId, accessorySave) --Save
	end)

	--print(accessorySave)
end)

You should slap a return on that data = inventoryDS:GetAsync(userId) statement. Try that and let me know if anything changes.

sadly still giving me nil for some reason