Buffer Out of Bounds

I’m somewhat new to buffers but I have ran in to an issue with them. Im using copy to copy the bytes from one buffer to another but I keep getting an buffer access out of bounds error.

I tried printing every slot in both buffers which are accessed just fine, but when I group them together in a single copy statement it’s out of bounds. Im clueless as to why.

			print("chars: ", numberOfInvalidChars,  outputBufferIncrement + 2 + numberOfInvalidChars, (n - 2) - (starting_n - 1))
			print("Input: ", n - 2, starting_n - 1, buffer.len(inputBuffer))

			-- Add info to buffer
			outputBuffer = writeToBuffer(outputBuffer, outputBufferIncrement + 2 + numberOfInvalidChars + 1, function(b: buffer)
				buffer.writeu8(b, outputBufferIncrement, 240) -- 240 to indicate extended ascii characters
				buffer.writeu8(b, outputBufferIncrement + 1, numberOfInvalidChars) -- the number of characters (up to 255)
				print("copy: ", buffer.len(b), outputBufferIncrement + 2, buffer.len(inputBuffer), starting_n - 1, n -2)
				buffer.writeu8(b, outputBufferIncrement + 2, 2)
				buffer.writeu8(b, outputBufferIncrement + 3, 2)
				buffer.writeu8(b, outputBufferIncrement + 4, 2)
				buffer.writeu8(b, outputBufferIncrement + 5, 2)
				buffer.writeu8(b, outputBufferIncrement + 6, 2)
				buffer.writeu8(b, outputBufferIncrement + 7, 2)
				print("sn-1", buffer.readu8(inputBuffer, starting_n - 1))
				print("sn",buffer.readu8(inputBuffer, starting_n ))
				print("sn+1", starting_n + 1, buffer.readu8(inputBuffer, starting_n + 1))
				print("n-3",n - 3, buffer.readu8(inputBuffer, n - 3))
				print("n-2",n - 2, buffer.readu8(inputBuffer, n - 2))
				--print("n-1", n-1, buffer.readu8(inputBuffer, n - 1)) -- errors like expected
				buffer.copy(b, outputBufferIncrement + 2, inputBuffer, starting_n - 1 , n - 2) -- Line 124, buffer access out of bounds
				return b
			end)

I first tried in VSCode running luau with the terminal, so I then tried studio and had the same issue.

Buffer Out of Bounds means you’re accessing a part of memory that is way off than the allocated buffer.
You need to check your calculations when copying the buffer, it should not exceed buffer.len()

I figured out why. I had my arguments mixed up.
I had done:

copy(target, targetOffset, source, sourceOffset, **ENDING POSITION**)

when the last argument should have been count.

copy(target, targetOffset, source, sourceOffset, count)

I was providing a range of bytes to copy instead of the number of bytes to copy.

idk how it took me 4+ hours to realize this