Had Vector3 been updated?

print(Vector3.new(1, 1, 1) == Vector3.new(1,1,1)) --It used to turn false, but now it's true

I remember this code didn’t return me true because Vector3 are unique things
But now it returns me true!
Also I couldn’t index table with vector3, but now I can. Does anyone know what happened?

I think you mistook vectors with tables.

But I remember that I couldn’t directly compare vectors using ‘==’(it gave me false even its xyz are just the same)
so I had to get distances between two instead

Honestly idk if they changed it, it could be just our memory going crazy (like with the seahorse emoji)

I think you are confusing this with Vector2, which had this behaviour (not sure if it still does or not). But for as long as I can remember Vector3 has acted completely different, comparing the XYZ values and not the class instance itself

Pretty sure it was always possible to do so

Vector3.new returns a table that somewhat looks like this

local myVector = {
   X: number = 5,
   Y: number = 4
   Z: number = 0,

   type: string = "vector" --// internal property
}

if ofc has methods and metamethods, it likely has __eq metamethod that compares values rather than reference itself

It does not return a table, Vector3 are userdata, which behave differently. You can check this by trying to use a table method on a Vector3. You could make a table with metamethods that make it act like a vector, though.

Vector3 is actually of the native vector type - this is a standalone data type in the language.

You can see this by checking in the console with type:
image

In the Luau VM, we can see the handler for comparisons under the any of the EQ opcodes, such as LOP_JUMPIFNOTEQ.

but in short, if we look at this section:

case LUA_TVECTOR:
    pc += !luai_veceq(vvalue(ra), vvalue(rb)) ? LUAU_INSN_D(insn) : 1;
    LUAU_ASSERT(unsigned(pc - cl->l.p->code) < unsigned(cl->l.p->sizecode));
    VM_NEXT();

this is basically saying, “if we’re comparing two vectors, call the function luai_veceq which will check if they are equal. If they are, go to the block of code for that case, if they aren’t then go somewhere else.”

Now, if we go to that function, we can see this line:

return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];

now what this does is compare the x, y and z of both vectors. If they all match, then it’ll return true. It’s doing this:

v1.x == v2.x and v1.y == v2.y and v1.z == v2.z

This is why when you put in the command bar:

print(Vector3.new(1, 1, 1) == Vector3.new(1, 1, 1))

you get true.

I hope this helps! Any questions please ask.

To answer @minus10degrees’s question exactly, yes, Vector3s are now a native type as of 4+ years ago:

Yes I know, I was inferring the key, value pair indexing, which is thanks to metatables

Could also use the FuzzyEq method to check for equality. It’s a lot cleaner and reliable because Vector3 values are floats and very often have tiny precision errors. And you could check for values that may look identical but will return false. It’s really useful for things like position, alignment, etc


documentation:
https://create.roblox.com/docs/reference/engine/datatypes/Vector3#FuzzyEq

I thought it had been impossible to use Vector3 as a dictionary key, as you can see in this post of mine:

But after a few minutes of googling, I found out that this has been possible since at least Apr 2022.
(There was someone who had a similar idea to mine)

I guess my code was so spaghetti that I might have mistaken it for not being able to index tables with Vectors.
Anyway I’m not sure about this but if it’s right then you would be right.

If there’s anyone who have idea about this please let me figure it out!

Its because a vector with the same components gives you the same object.

They’re memoized in a way so that you only create one copy of each vector, and then the constructor gives you the same one.

You should move to using native vectors, rather than Vector3, because even though they’re the same under the hood, native vectors can take advantage of fast calling, while the legacy type needs to use namecalling.

For most people there will not be an observable difference. Even the announcement for the vector library does not really recommend this.

I think this is right.
Vector3 and 2 work differently when being used as a key in a table.

--Vector3
local test = {[Vector3.new(-1, 2, 3)] = "Hello"}
print(test [Vector3.new(-1, 2, 3)]) -- prints Hello

--Vector2
local test = {[Vector2.new(-1, 2)] = "Hello"}
print(test [Vector2.new(-1, 2)]) -- prints nil

I wish Vector2 had the same behavior as Vector3