Trying to get table from datastore

I’m trying to get a table from a datastore. I’m saving the table using this:

game.Players.PlayerRemoving:Connect(function(plr)
	local parts = {}

	for i, v in pairs(game.Workspace.Parts:GetChildren()) do
		if parts[v.Name] == nil then
			parts[v.Name] = {}
		end

		parts[v.Name]["ID"] = v.PartID.Value

		print(parts)
	end
	
	local success, errormessage = pcall(function()
		partDS:SetAsync(plr.UserId.."Parts", parts)
	end)

	if success then
		print("Data successfully saved")
	else
		print("Error when saving data")
		warn(errormessage)
	end
end)

I am trying to load the data using this:

game.Players.PlayerAdded:Connect(function(plr)
	local data
	local success, errormessage = pcall(function()
		data = partDS:GetAsync(plr.UserId.."Parts")
	end)

	if success then
		for i, v in pairs(data) do
			print(game.ServerStorage.Parts:FindFirstChild(data[i].Name).Name)
		end
	else
		warn(errormessage)
	end
end)

I get the error “Argument 1 missing or nil” when trying to print the name if the item in serverstorage. I do not know why this is. All help appreciated.

If the name of the part is the ID property.

print(game.ServerStorage.Parts:FindFirstChild(data[i].ID).Name)

or it could be

print(game.ServerStorage.Parts:FindFirstChild(i).Name)

not sure how your system works.

2 Likes