When I try a simple convert, it fails... table to vector
local t = {4,5,6} local v = Instance.new("Vector3Value", workspace) v.Value = Vector3.new(t) 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:
local vector = {1,2,3} function newVector(table) if(#table == 3) then --The table has a length of 3 and is an object array. Now let's make the vector 3 return Vector3.new(table[1],table[2],table[3]) end --Return a blank vector3 as invalid parameters were supplied return Vector3.new(0,0,0) end print(newVector(vector).x) print(newVector(vector).y) 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!
local vector = Vector3.new(1,2,3) function vectorToTable(vector) return {vector.x,vector.y,vector.z} end print(vectorToTable(vector)[3]) print(vectorToTable(vector)[2]) 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.
--option 1 v.Value = Vector3.new(t[1], t[2], t[3]) --option 2 v.Value = Vector3.new(unpack(t))