How can i create optimized UUID?

When using OOP i need to have unique IDs for each projectile, gun and ect., also i need to send it over the network

But if i do this

Remote:FireAllClients(self.ID)

And ID was generated by HTTP service, it would send 34 bytes over the network, which is huge, even using unreliable remote events wouldn’t help if i would send kilobytes of data over the network every frame, making game unplayable

Is there any alternative to HTTP service which doesn’t take that much memory? also doesn’t take a lot to store? any ideas?

1 Like

why not make it a number? numbers are 8 bytes, and you can use the buffer library to send 1/2/4/8 bytes

also, I don’t think unreliable remotes are good for this because they are unreliable

1 Like

You’re joking right? :skull: They’re called unreliable because they don’t do intensive checks to make sure the data gets to the other side which means some data may get dropped but it’s really perfomant

1 Like

This resource may work for your use case:

From inside the source code you can change the length of the UUID (by adding or removing the "x"s in the uuid function. This also has the advantage of checking if your UUID is actually unique, unline roblox GUID from HTTP Service.

and that is why they are unreliable
they shouldn’t be used for important gameplay stuff in the game

1 Like

He also mentioned projectiles, which will probably get fired many times meaning it’ll cause a strain on the network to keep validating stuff

2 Likes

Indeed i realized if every heartbeat projectile’s position will be overwritten, losing some packets wouldn’t be that bad

I tried using numbers but i had to make active/inactive Id’s list, they took some space, let’s say 200 bullets were created at the same time, in this system they will be saved to a table, although if new bullets were made and those previous bullets were destroyed, it wouldn’t add any new registry, if let’s say someone fired a lot of bullets, tables will be crowded

1 Like

you can use an 8-byte number which is really big (2^63 ig)
and if it’s a game with rounds you can reset the id to 0 and remove all the bullets after each round
and if it’s not you can remove all bullets once the id is 2^63 which is again a very big number

there might be better solutions that this, but this is what though of

-Server-
id = 0

new bullet:
 set bullet.Id =  id
 Id += 1
 
 if id == 2^63 then
  reset id and send remote to client to remove all bullets
 end

 send(id)

-Client-
bullets = {}

onRecive(id):
 local bullet Instance.new("Bullet")
 extra stuff
 bullets[id] = bullet

on update Position(id,Positon):
 bullets[id].Positon = Position

onGameEndOrWhenIdIs 2^63:
remove all bullets from table

Using a buffer is a perfect solution. You can generate a UUID-sized buffer like this:


local function GenerateUUIDBuffer(): buffer
	local UUIDRandom: Random = Random.new()
	local UUIDBuffer: buffer = buffer.create(16)
	local UUIDByte: number
	for i=0, 15 do
		UUIDByte = UUIDRandom:NextInteger(0, 255)
		buffer.writeu8(UUIDBuffer, i, UUIDByte)
	end
	return UUIDBuffer
end

local function tostringUUIDBuffer(UUIDBuffer: buffer): string
	local UUIDFormatString: string = "%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x"
	local UUIDBytes: {number} = {}
	local UUIDByte: number
	for i=0, 15 do
		UUIDByte = buffer.readu8(UUIDBuffer, i)
		table.insert(UUIDBytes, UUIDByte)
	end
	return string.format(UUIDFormatString, table.unpack(UUIDBytes))
end
1 Like

(P.S: This was kinda fun to code!)

id += 1

i doubt in the whole match / round there will be long enough for everyone to shoot even close to a million bullets

you can just count the max amount of players your server has * the most ammo a weapon can have

1 Like