Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

table to vector is giving 0,0,0 every time I convert it, any help?

Asked by 6 years ago

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

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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!

1
I may need a tiny more help, I saved a vector to my datastore using ToString, these methods arent working for that reason greatneil80 2647 — 6y
0
Sure! Check the edited answer! ArtsicleOfficial 171 — 6y
0
I am still getting 0,0,0 D: greatneil80 2647 — 6y
0
suppose it is "1", "2", "3" what should I do then? greatneil80 2647 — 6y
View all comments (6 more)
0
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 ArtsicleOfficial 171 — 6y
0
also once you got it in a table all you do is set it in your datastore. tables work in datastores ArtsicleOfficial 171 — 6y
0
0,0,0 lets talk on codeshare greatneil80 2647 — 6y
0
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 ArtsicleOfficial 171 — 6y
0
codeshare is a link that sends you to my code greatneil80 2647 — 6y
Ad
Log in to vote
2
Answered by
amanda 1059 Moderation Voter
6 years ago
Edited 6 years ago

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))
0
Heck.... I somehow saved it as a vector ._. greatneil80 2647 — 6y

Answer this question