Datastore saving not working

Im trying to add an inventory save to my datastore script but i keep getting this error
ServerScriptService.DataStore:65: attempt to call missing method ‘GetChildren’ of table

local plrs = game:GetService("Players")
local ds = game:GetService("DataStoreService")

local datastore = ds:GetDataStore("PlrData")

local currentdata = {}
function playerjoined(plr)
	local playerStats = Instance.new("Folder")
	playerStats.Name = "Data"

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = playerStats
	local rank = Instance.new("StringValue")
	rank.Name = "Rank"
	rank.Parent = playerStats
	local xp = Instance.new("NumberValue")
	xp.Parent = playerStats
	xp.Name = "XP"
	local inventory = Instance.new("Folder")
	inventory.Parent = playerStats
	inventory.Name = "Inventory"
	playerStats.Parent = plr
	local success = nil
	local playerdata = nil
	local attempt = 1
	repeat
		success, playerdata = pcall(function()
			return datastore:GetAsync(plr.UserId)

		end)

		attempt += 1
		if not success then
			warn(playerdata)
			task.wait(2)
		end
	until success or attempt == 5
	if success then
		print("data retrieved for ", plr.Name)
		if not playerdata then
			print("assigning data")
			playerdata = {
				['Money'] = 0,
				['XP'] = 0,
				['Inventory'] = {}
			}
		end
		currentdata[plr.UserId] = playerdata
	else
		warn("unable to load data for ", plr.Name)
		plr:Kick("Not able to load your data, try again later", plr.Name)
	end
	money.Value = currentdata[plr.UserId].Money
	xp.Value = currentdata[plr.UserId].XP
	xp.Changed:Connect(function()
		currentdata[plr.UserId].XP = xp.Value
	end)
	money.Changed:Connect(function()
		currentdata[plr.UserId].Money = money.Value
	end)

	-- Reload player inventory from datastore
	local playerInventory = currentdata[plr.UserId].Inventory
	for _, item in pairs(playerInventory:GetChildren()) do
		item.Parent = plr.Backpack
	end
end


plrs.PlayerAdded:Connect(playerjoined)
function playerLeft(player)
	if currentdata[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1

		-- create a table to store the player's inventory
		local inventoryTable = {}

		-- loop through the player's backpack items and add them to the table
		for _, item in ipairs(player.Backpack:GetChildren()) do
			table.insert(inventoryTable, item.Name)
		end

		-- add the inventory table to the player data
		currentdata[player.UserId].Inventory = inventoryTable

		repeat
			success, errorMsg = pcall(function()
				datastore:SetAsync(player.UserId, currentdata[player.UserId])

				-- clear the player's backpack
				player.Backpack:ClearAllChildren()

				-- loop through the inventory table and add the items back to the backpack
				for _, itemName in ipairs(currentdata[player.UserId].Inventory) do
					local item = game:GetService("ServerStorage"):FindFirstChild(itemName)
					if item then
						item:Clone().Parent = player.Backpack
					end
				end
			end)
			attempt += 1
			if not success then
				warn(errorMsg)
				task.wait(2)
			end
		until success or attempt == 5
		if success then
			print("data saved for ", player.Name)
		else
			warn("unable to save data for ", player.Name)
		end
	end
end

You were trying to get the Inventory’s children but since “Inventory” is a table, you instead get the error. Change this :

local playerInventory = currentdata[plr.UserId].Inventory
for _, item in pairs(playerInventory:GetChildren()) do
	item.Parent = plr.Backpack
end

To this :

local playerInventory = currentdata[plr.UserId].Inventory
for _, item in pairs(playerInventory) do
	item.Parent = plr.Backpack
end
1 Like