How would I rearrange a pixel array for :WritePixels() correctly?

So I have a canvas that is supposed to render out chunks of pixels generated from a 2D array. everything works fine, except It’s not arranged in a correct order.


(Each color represents a chunk, with different shades representing the pixel/block inside of it. Red part is used to indicate the top-left corner of canvas.)

Here’s what it’s supposed to look like:

image

code used:

local chunkSize = 2;
local worldSize = 2;
local worldPxSize = (chunkSize*worldSize);

local part = script.Parent;
local surfaceGui = part.SurfaceGui;
local eImage = Instance.new("EditableImage");

eImage.Parent = surfaceGui.ImageLabel;

local chunks = {};

local function genChunkPixels( chunkPos: Vector2 , chunkIndex: number )
	local texture = {};
	
	local pxIndex = 0;
	
	for pxX = 1,chunkSize do
		for pxY = 1,chunkSize do
			
			pxIndex += 1;
			
			local val = (pxIndex^.5)/chunkSize;
			
			local color = Color3.fromHSV(chunkIndex/(worldSize^2),1,val);
			local data = {color.R;color.G;color.B;1};
			
			for i=1,4 do
				table.insert(texture,data[i]);
			end;
		end;
	end;
	
	return texture;
end;

local pixels = {};

local chunkIndex = 0;

for x = 1,worldSize do
	for y = 1,worldSize do
		
		chunkIndex += 1;
		
		local tex = genChunkPixels(Vector2.new(x,y),chunkIndex);
		
		for _,v in pairs(tex) do
			table.insert(pixels,v);
		end;
	end;
end;

local size = Vector2.one * worldPxSize;

eImage.Size = size;

eImage:WritePixels(Vector2.zero,eImage.Size,pixels);

Anybody have any ideas on how to achieve this? I’ve been trying to figure out the algorithm for quite a while now and I’m still not sure on how I would do it.