Learning buffers

I’m trying to use buffers for when I fire unreliables to the client but I think I have to cancel or destroy its creation as its “mutable block of memory”, I’m just not sure how.

Would just setting the buffer to nil automatically remove it and its contained memory?

InfoSignaler:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local library = ReplicatedStorage.Library

local bufferService = require(library.BufferService)

local function returnRandomAngle()
	local newBuffer = bufferService.Create()
	local x = bufferService.writei16(newBuffer,math.rad(math.random(-360,360)))
	local y = bufferService.writei16(newBuffer,math.rad(math.random(-360,360)))
	local z = bufferService.writei16(newBuffer,math.rad(math.random(-360,360)))
	return CFrame.Angles(x,y,z)
end

local InfoSignaler = {}

InfoSignaler["Metal Bat"] = function(tool)
	return {CFrame = tool.Hitbox.CFrame * returnRandomAngle()}
end

return InfoSignaler

BufferService:

--!strict

export type run = {
	Create: (size: number) -> (),
	writeString: (bfr: buffer,value:string) -> (),
	writei16: (bfr: buffer,value:number) -> (),
}

local BufferService = {} :: run

BufferService.Create = function(size)
	local newBuffer = buffer.create(size)
	return newBuffer
end

BufferService.writeString = function(bfr,value)
	buffer.writestring(bfr,0,value)
	return buffer.readstring(bfr,0,string.len(value))
end
BufferService.writei16 = function(bfr,value)
	buffer.writei16(bfr,0,value)
	return buffer.readi16(bfr,0)
end

return BufferService
2 Likes

Hey TheOtherSider,
I am also learning buffers. Setting the buffer to nil would not automatically remove it from the memory. LuaU is a garbage-collected language. This means that LuaU automatically manages the memory for you. Setting the buffer to nil will only tell LuaU that you’re done with it.
But please, I am not John LuaU. Do your own research.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.