Table not saving to datastore

I have created a simple script for my game which is supposed to save the players data into the inventory datastore. The only issue is that it doesn’t even run the code that is supposed to save the players inventory data.

game.Players.PlayerRemoving:Connect(function(player)
	--print(player.Name .. " is leaving")
	--xpDS:SetAsync(player.UserId, player.playerStats.xp.Value)
	--print("XP Saved")
	--levelDS:SetAsync(player.UserId, player.playerStats.level.Value)
	--print("Level saved")
	--fundsDS:SetAsync(player.UserId, player.playerStats.funds.Value)
	--print("Funds saved")
	local playerInventory = {
		["Flashlight"] = player.Inventory.Flashlight.Value,
		["InfraredThermometer"] = player.Inventory.InfraredThermometer.Value,
		["Glowstick"] = player.Inventory.Glowstick.Value,
		["SpiritBox"] = player.Inventory.SpiritBox.Value,
		["EMF"] = player.Inventory.EMF.Value,
		["MotionSensor"] = player.Inventory.MotionSensor.Value,
		["TracerLightWire"] = player.Inventory.TracerLightWire.Value,
		["Lighter"] = player.Inventory.Lighter.Value,
	}
	local key = player.UserId
	
	print(playerInventory)
	print("Attempting to save inventory for " .. player.Name)
	local success, response = pcall(inventoryDS.SetAsync, inventoryDS, key, playerInventory)
	if success then
		print("Successfully saved inventory for " .. player.Name)
	else
		print("Failed to save inventory for " .. player.Name .. "(" .. response .. ")")
	end
end)

It never says that the data failed to save or load, I am completely lost and have absolutely no idea why this is occurring.

game.Players.PlayerRemoving:Connect(function(player)
	--print(player.Name .. " is leaving")
	--xpDS:SetAsync(player.UserId, player.playerStats.xp.Value)
	--print("XP Saved")
	--levelDS:SetAsync(player.UserId, player.playerStats.level.Value)
	--print("Level saved")
	--fundsDS:SetAsync(player.UserId, player.playerStats.funds.Value)
	--print("Funds saved")
	local playerInventory = {}
	for _,tool in player.Invetory:GetChildren() do
		if tool then
			playerInventory[tool.Name] = tool.Value
		end
	end
	
	local key = player.UserId
	
	print(playerInventory)
	print("Attempting to save inventory for " .. player.Name)
	local success, response = pcall(inventoryDS.SetAsync, inventoryDS, key, playerInventory)
	if success then
		print("Successfully saved inventory for " .. player.Name)
	else
		print("Failed to save inventory for " .. player.Name .. "(" .. response .. ")")
	end
end)

I suggest you try to print a statement after the if-else statement to see if the problem still persists.

This issue has been resolved, the problem was I didn’t put the pcall into a function.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.