When I try a simple convert, it fails... table to vector
1 | local t = { 4 , 5 , 6 } |
2 |
3 | local v = Instance.new( "Vector3Value" , workspace) |
4 | v.Value = Vector 3. new(t) |
5 | print (v.Value) |
Instead it converts 456 into 0,0,0 when I want it to be 4,5,6
I don't recall ever being able to use tables in the constructor for Vector3s, maybe I'm wrong, but I don't think it's there. Here's a quick workaround:
01 | local vector = { 1 , 2 , 3 } |
02 | function newVector(table) |
03 | if (#table = = 3 ) then |
04 | --The table has a length of 3 and is an object array. Now let's make the vector 3 |
05 | return Vector 3. new(table [ 1 ] ,table [ 2 ] ,table [ 3 ] ) |
06 | end |
07 | --Return a blank vector3 as invalid parameters were supplied |
08 | return Vector 3. new( 0 , 0 , 0 ) |
09 | end |
10 |
11 | print (newVector(vector).x) |
12 | print (newVector(vector).y) |
13 | print (newVector(vector).z) |
We created a function to help make vectors, called newVector. We can now use that to make vectors whenever we want easily. Output:
1
2
3
If you need anymore help just ask! :D
Edit: To convert it to a table again to put it in a datastore, just make a function that converts it back to a table!
1 | local vector = Vector 3. new( 1 , 2 , 3 ) |
2 | function vectorToTable(vector) |
3 | return { vector.x,vector.y,vector.z } |
4 | end |
5 |
6 | print (vectorToTable(vector) [ 3 ] ) |
7 | print (vectorToTable(vector) [ 2 ] ) |
8 | print (vectorToTable(vector) [ 1 ] ) |
Output:
3 2 1
So now all you need to do is convert it to a table and then store it in the datastore!
A table is not a valid argument for the Vector3.new
constructor.
You can either give it no arguments, where it will return an empty Vector3
, or you can give it 3 numbers, and it will return a Vector3
based off of those numbers.
Giving it a table is not the same as giving it 3 numbers, you are only giving it one argument of an invalid type and it is defaulting on you.
To fix this, index
the table 3 times, or unpack
the table to release it as 3 arguments.
1 | --option 1 |
2 | v.Value = Vector 3. new(t [ 1 ] , t [ 2 ] , t [ 3 ] ) |
3 |
4 | --option 2 |
5 | v.Value = Vector 3. new( unpack (t)) |