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
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))
Heck.... I somehow saved it as a vector ._.
This discussion is preserved as it was. New answers, comments, and votes are closed.
10 comments
I may need a tiny more help, I saved a vector to my datastore using ToString, these methods arent working for that reason
Sure! Check the edited answer!
I am still getting 0,0,0 D:
suppose it is "1", "2", "3" what should I do then?
Try doing a print() of your table values like print(table[1]) print(table[2]) print(table[3]) and tell me what it results in
also once you got it in a table all you do is set it in your datastore. tables work in datastores
0,0,0 lets talk on codeshare
https://codeshare.io/aVy1l3
Try editing your answer with more info, we don't know why the vector is 0,0,0. idk what codeshare is, and we can easily fix this with sh
codeshare is a link that sends you to my code