Buffer not working when loading a unsigned integer

Create a chunk system using BitBuffer (https://devforum.roblox.com/t/bitbuffer-module-for-binary-data-manipulation/8315/4) to save data.

The issue is I get a error from the module, here’s the error if you want: ReplicatedStorage.Chunk.BitBuffer:314: attempt to perform arithmetic (mul) on nil and number

I tried fixing the issue in the BitBuffer module but nothing worked.

Module:

--Yeah, a chunk handler

local BitBuffer = require(script:WaitForChild("BitBuffer"))

local Chunk = {}
Chunk.CurrentChunk = 1 --Default spawn chunk
Chunk.Buffer = BitBuffer.Create()

--Read/Write system
function Chunk:WriteCurrentChunk()
	--Writes a chunk to the buffer via unsigned intergers.
	--Unsigned doesn't allow us to have negative numbers but give us more space.
	local Buffer = Chunk.Buffer
	
	Buffer:WriteUnsigned(8, Chunk.CurrentChunk) --8 bit(255 max) because we don't have alot of chunks(4 billon of them).
	
	print("Writed current chunk to buffer.")
end

function Chunk:ReadLastChunk()
	--Reads the chunk in the buffer.
	--Sets the the chunk returned from reading.
	local Buffer = Chunk.Buffer
	
	Chunk.CurrentChunk = Buffer:ReadUnsigned(8) --Reads the 8 bit(255 max) unsigned interger we wrote earlier.
	
	print("Setted current chunk to "..tostring(Buffer:ReadUnsigned(8))..".")
end

return Chunk
1 Like

This is because you are required to reset the buffer pointer via the BitBuffer:ResetPtr() method.
Otherwise, the pointer will reside at the end of the buffer, and upon reading will read outside of the buffer, causing this error.

2 Likes

I tried using this method but it still gave me the same error when calling before reading:

--Yeah, a chunk handler

local BitBuffer = require(script:WaitForChild("BitBuffer"))

local Chunk = {}
Chunk.CurrentChunk = 1 --Default spawn chunk
Chunk.DefaultChunk = 1 --Default spawn chunk when reseted
Chunk.Buffer = BitBuffer.Create()
Chunk.IDSet = {
	[0] = {script.Parent:WaitForChild("chunks").Chunk1_test, true},
	[1] = {script.Parent:WaitForChild("chunks").yourhomef2, true},
}

--Read/Write system
function Chunk:WriteCurrentChunk()
	--Writes a chunk to the buffer via unsigned intergers.
	--Unsigned doesn't allow us to have negative numbers but give us more space.
	local Buffer = Chunk.Buffer
	
	Buffer:ResetPtr()
	Buffer:WriteUnsigned(8, Chunk.CurrentChunk) --8 bit(255 max) because we don't have alot of chunks(4 billon of them).
	print("Writed current chunk to buffer.")
end

function Chunk:ReadLastChunk()
	--Reads the chunk in the buffer.
	--Sets the the chunk returned from reading.
	local Buffer = Chunk.Buffer
	
	Buffer:ResetPtr()
	Chunk.CurrentChunk = Buffer:ReadUnsigned(8) --Reads the 8 bit(255 max) unsigned interger we wrote earlier.
	print("Setted current chunk to "..tostring(Buffer:ReadUnsigned(8))..".")
end

function Chunk:ResetChunkData()
	--WARNING:YOU CAN NOT RESTORE DATA ONCE CALLING THIS!
	--Uses the buffer to reset it and set the current chunk to 1
	local Buffer = Chunk.Buffer
	
	Buffer:ResetPtr()
	Buffer:Reset()
	Chunk.CurrentChunk = Chunk.DefaultChunk
	print("Reseted chunk data, data can not be restored!")
end

--Load/Destroy system
function Chunk:LoadChunk(ID)
	--Doesn't read data to get the chunk, instead it the id you give will load the chunk.
	local lastTick = tick()
	Chunk.CurrentChunk = ID
	Chunk:WriteCurrentChunk()
	local ChunkClone = Chunk.IDSet[ID][1]:Clone()
	ChunkClone.Name = "ChunkLoaded"
	ChunkClone.Parent = workspace
	print("Loaded chunk time:"..math.abs(lastTick-tick()).."")
end

function Chunk:GetChunk()
	--Reads the last chunk via ReadLastChunk, usefull for respawning the player
	local lastTick = tick()
	Chunk:ReadLastChunk()
	local ChunkClone = Chunk.IDSet[Chunk.CurrentChunk][1]:Clone()
	ChunkClone.Name = "ChunkLoaded"
	ChunkClone.Parent = workspace
	print("Get chunk time:"..math.abs(lastTick-tick()).."")
end

function Chunk:DestroyChunk()
	--TODO: Write this!
end

return Chunk

You did two ReadUnsigned in the ReadLastChunk, so the first one moves the pointer, then the other read reads outside of the buffer.
Its in the print statement saying “Setted current chunk to”