This tutorial will show you how to add or subtract Vector3 values.

- Insert a Vector3Value.
- Change the value of Vector3Value to (15, 15, 15).
- Click on Vector3Value.
- Insert a script. Make sure the script's Parent is the Vector3Value.

Once those parts are done, open the script. First, we need to identify the value we want to change. So put in this (note that *local* is not needed in a basic script, but ones that use functions may need it):

local value = script.Parent.Value

We now can read the x, y, and z coordinates of the Vector3 value. Let's say we want to add 5 to the value along all three axes. There are two ways to do this. If there are more complicated calculations to be made, it will be better to do it this way:

local x, y, z = value.x, value.y, value.z

Although we have the 3 seperate numbers, we still haven't added or subtracted them yet. In order to do that, you just have to add and subtract it like you do with regular numbers.

x, y, z = x + 5, y + 5, z + 5

Now we have the new x, y, and z. You could do extensive calculations on the x, y, and z variables, and you can treat them differently. You might multiply x by 2, for example, while leaving the others the same.

Note that although we have changed the values of the coordinates, we have not actually changed the Vector3Value. To do this, we need one final line:

script.Parent.Value = Vector3.new(x, y, z)

Let's take a look at what we have so far for this method:

local value = script.Parent.Value local x, y, z = value.x, value.y, value.z x, y, z = x + 5, y + 5, z + 5 script.Parent.Value = Vector3.new(x, y, z)

Now the value has changed. But how will we know? We just need to put in another line. Make sure you have output up first:

print(script.Parent.Value)

The script is complete! Let's just look at the finished script:

local value = script.Parent.Value local x, y, z = value.x, value.y, value.z x, y, z = x + 5, y + 5, z + 5 script.Parent.Value = Vector3.new(x, y, z) print(script.Parent.Value)

Output should show `20, 20, 20`. Each time you run the script, the coordinates will change again.

Usually there will be no need to keep track of all three individual coordinates. In this case, only one line is needed:

script.Parent.Value = script.Parent.Value + Vector3.new(5, 5, 5)

In this example, you change the Vector3.new(5, 5, 5) to anything you like, including negatives. This provides an alternative method to subtraction shown below.

We've added them, but we now subtract them! Subtracting them is just like adding them, only we one line.

x, y, z = x - 5, y - 5, z - 5

Now, let's take a look at that edit in the full script:

local value = script.Parent.Value local x, y, z = value.x, value.y, value.z x, y, z = x - 5, y - 5, z - 5 script.Parent.Value = Vector3.new(x, y, z) print(script.Parent.Value)

Output should show `10, 10, 10`. You're done with subtracting them!

This gets a bit more tricky to do. Of course, you can always use the system used above, and get something like:

local value = script.Parent.Value local x, y, z = value.x, value.y, value.z x, y, z = x*2, y*2, z*2 script.Parent.Value = Vector3.new(x,y,z) print(script.Parent.Value)

...but sometimes one line is better to look at than 8. So this time, we will multiply the Vector3 value itself instead of its individual values.

To do this, either multiply it by a number (x, y, and z will all be multiplied by this) or multiply it by another Vector3 value (x, y, and z from your original Vector3 will be multiplied with the respective number in the new Vector3). A look at how this works:

local value = script.Parent.Value print(value) --Yes, you can print this way also. script.Parent.Value = value * 2 print(script.Parent.Value)

This script multiplies everything in the Vector3 value by the same number, which is 2 in this case.

This is useful in several cases, such as vehicle tools. But let's say we want to only multiply x and y by 2 and get rid of z, making it 0. We could define each value separately and multiply it out like we did above, or we could do this:

print(script.Parent.Value) local value = script.Parent.Value value = value * Vector3.new(2, 2, 0) print(value)

This script may seem a bit hard to comprehend at first, but the basic premise is this: it takes both x values and multiplies them, then both y values, then both z values. The value becomes the result of each of those products; in this case, x and y are doubled and z is now 0. Also, we can multiply the variable `value` by a Vector3 and not have to worry about the script breaking.

Dividing also works the same way. Just use / instead of *.

print(script.Parent.Value) local value = script.Parent.Value value = script.Parent.Value / 2 print(value) value = value / Vector3.new(2, 1, 0) print(value)

Now test this script and... what's that? It spits out INF for z (or NAN if it started as 0) at the end? Well, that would be because a number was divided by 0. Do try to avoid that. Remember that if you do not wish to change a coordinate, divide it by 1 to keep it the same.

**Instructions**

Make a function that returns the distance between two **Vector3** positions.

Solution |
---|

Select
function calculateDistance(position1, position2) return (position1 - position2).magnitude end |