Buffer type confusion (reading a different/unexpected value)

Ever since the new introduction of the new buffer type I’ve been experimenting with it, but I’ve ran into a problem (or what could be my own stupidity lol). Here is some code:

local b = buffer.create(10) --
buffer.writei16(b, 0, 100)

print(buffer.readi16(b, 0))

This piece of code expectedly prints 10 (which it does), but this is not where the problem occurs. It is when I add another value at a different offset where things start getting confusing for me:

local b = buffer.create(10)
buffer.writei16(b, 0, 100)
buffer.writei16(b, 1, 101)

print(buffer.readi16(b, 0)) --25956 ???
print(buffer.readi16(b, 1)) --101

For whatever reason, when I try to read the value at 0, it returns some confusing number. In this case it returns 25956. The weird thing though is when I change the offset from 1 to 2 for the second value, the code works normally again. From what it seems I have to keep atleast one space free between values I want to write to keep them from “corrupting”, “changing”, or whatever you would call this occurance.

local b = buffer.create(10)
buffer.writei16(b, 0, 100)
buffer.writei16(b, 2, 101)

print(buffer.readi16(b, 0)) --100
print(buffer.readi16(b, 1)) --101
-- Problem solved?

Edit: I’ve also decided to test with writei8/readi8 and it seems to work perfectly fine
Edit 2: It for some reason also happens for f32 but this time you need 3 gaps between two values for some reason. I have no idea why this is happening, perhaps my testing could be just bad.

a signed 16 bit integer contains 2 bytes.

the correct way to do this is:

local buff = buffer.create(4) -- // 4 bytes
local cursor = 0

buffer.writei16(buff, cursor, 9)
cursor += 2 -- // 2 bytes

buffer.writei16(buff, cursor, 102)
cursor += 2

local valueFromBuffer = buffer.readi16(buff, 0)
local value2FromBuffer = buffer.readi16(buff, 2)

print(valueFromBuffer, value2FromBuffer) -- // output: 9, 102

Ahh I see now.
So this means:

8-bit = 1 byte
16-bit = 2 bytes
32-bit = 4 bytes
64-bit = 8 bytes

and I would therefore have to change the offset by the size of the byte, right?

Yes, that is correct.

and just incase you post for writing strings to a buffer, each character is 1 byte, so to send it over a remote properly would be something like this:

local remoteEvent = ...
local player = ...

local stringToSend = "...hello fellas"
local stringLength = string.len(stringToSend)

local buff = buffer.create(stringLength + (1)) -- // a extra byte to store the length of the string
local cursor = 0

buffer.writeu8(buff, cursor, stringLength)
cursor += 1

buffer.writestring(buff, cursor, stringToSend, stringLength)
cursor += stringLength

remoteEvent:FireClient(player, buff)

-- // ON THE CLIENT:

local cursor = 0

local stringLength = buffer.readu8(buff, cursor)
cursor += 1

local stringSentFromServer = buffer.readstring(buff, cursor, stringLength)
cursor += stringLength

print(stringSentFromServer)