Is there a way to convert a .Magnitude back to a Vector3? (Serialization)

local Magnitude =, 1, 1).Magnitude --> 1.73205...

Any way I can turn Magnitude back into a Vector3?

Short answer is no. I could get a similar magnitude with:



Aw unfortunate. My intention was for serialization but I’ll probably have to just do something like v=1,1,1.

Is there a max size for the numbers? Using binary may save on space.

The max vector size my code would allow is something like, 250, 250). I was planning to serialize it like v=250,250,250. How could I use binary to make that smaller, if it’s possible?

Just do {250, 250, 250}. And no, binary won’t shorten it.

1 Like

Binary would shorten it as any number from 0-255 would just be a single character. That means it could be stored as 3 characters instead of all of the extra characters for the commas and brackets.

Would this use string.char or something similar?

A vector is a value with direction and magnitude. You can turn a magnitude back into a vector, but only if you also have a direction to give it.

local direction = vec.Unit --(Vector3 with a magnitude of 1)
local magnitude = vec.Magnitude
local result = direction * magnitude

That’s not going to help you at all though because it’s going to take up more space than a regular Vector3.
A Vector3 should only take up to 24 bytes of space for the data itself.

If you’re using small whole numbers such as 250, it’s possible to keep it the way you have it there and save a small, negligible amount of space. If you’re using decimals or four digit numbers though, it’s far more proper to just use string.pack. 24 bytes isn’t very many.
And that’s only if you must save it as a string. Otherwise do what @lolmansReturn suggested.

You can’t turn a binary digit into 256 different possibilities. That takes a byte. You can store a byte as a character though by using things like string.char and string.byte (and I assume that’s what you meant), but again I’d refer you to string.pack.


Yes, that would work. However, larger numbers would require your own system.

What do you think binary is? Binary can be converted into a number or byte character. Binary is just 0’s and 1’s. A bit is one 0/1, and a byte is 8 bits. Bits and bytes are all binary. Assigning a character to them is just one way of representing them.

1 Like

You’re right, I’ve just never heard anyone refer to serialization that way. I just say ‘store it as chars’ or something like that. But I understand now what you mean. (And again I think string.pack is super useful for this)

Noobie question but for string.pack, it’s asking for a format. What should I keep in mind for making my own format? Or is it simpler than that?

Sorry, I forgot Roblox doesn’t have documentation on it.
This is from the Lua reference manual.

In our case we want either f or d. d is for double, a 64 bit number. It’s a lot of data. If you don’t require your data to be precise to 0.0000001 studs or whatever, and your data won’t be absurdly large such as in the upper millions of studs, you only need f which is half the size.
The pattern should just be this, times however many you want to store. A Vector3 has three dimensions, so we want three per vector.

local packed_string = string.pack("fff", vec.X, vec.Y, vec.Z)
local x, y, z = string.unpack("fff", packed_string)

If you have more data, you can store it in the same string.

local packed_string = string.pack(
    vec1.X, vec1.Y, vec1.Z,
    vec2.X, vec2.Y, vec2.Z,
local v1x, v1y, v1z, v2x, v2y, v2z, tsd, fit = string.unpack("ffffffff", packed_string)
1 Like

Pretty valuable information here, thank you. Unfortunate that this isn’t on the docs site.

1 Like

Yeah, pack and unpack open up major possibilities, but no one really knows how they work.


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