How To Increase and Decrease Vector3 Values: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Bob10110
No edit summary
Adding ScriptTutorial template
 
(13 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{CatUp|Tutorials}}
{{CatUp|Tutorials}}
__TOC__
{{ScriptTutorial|easy|scripting}}
 
==Introduction==
== Introduction ==
This tutorial will show you how to add or subtract {{type|Vector3}} values. This will be quite a benefit to your scripts, and will make them more popular if you have them.
 
This tutorial will show you how to add or subtract Vector3 values. This will be quite a benefit to your scripts, and will make them more popular if you have them.
 
==Adding Vector3 Values==
==Adding Vector3 Values==
 
# Insert a {{type|instance=Vector3Value}}.
1) Insert a Vector3Value.<br>
# Change the value of {{type|instance=Vector3Value}} to (15, 15, 15).
 
# Click on {{type|instance=Vector3Value}}.
2) Change the value of Vector3Value to 15, 15, 15.
# Insert a script. Make sure the script's [[Parent (Property)|Parent]] is the {{type|instance=Vector3Value}}.
 
3) Click on Vector3Value.
 
4) Insert a script. Make sure the script's parent is the Vector3Value.
 
== Scripting ==
== Scripting ==
 
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):
Once those parts are done, open the script. First, we need to identify the value we want to change. So put in this.
<syntaxhighlight lang="lua">local value = script.Parent.Value</syntaxhighlight>
 
We now can read the x, y, and z coordinates of the {{type|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:
<pre>
<syntaxhighlight lang="lua">local x, y, z = value.x, value.y, value.z</syntaxhighlight>
local value = {script.Parent.Value}
</pre>
 
The value is whatever value you put. Next, we need to put an i = 1 part. I don't know why we have to do this, but we have to.
 
<pre>
local i = 1
</pre>
 
Let's look at our progress:
 
<pre>
local value = {script.Parent.Value}
local i = 1
</pre>
 
Since we have those 2 lines down, we now can tamper into the x, y, and z of the Vector3 Value. Let's say we want to add 5 to the value. Put in these lines:
 
<pre>
local x = value[i].x
local y = value[i].y
local z = value[i].z
</pre>
 
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.
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.
<syntaxhighlight lang="lua">x, y, z = x + 5, y + 5, z + 5</syntaxhighlight>
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.


<pre>
Note that although we have changed the values of the coordinates, we have not actually changed the {{type|instance=Vector3Value}}. To do this, we need one final line:
x = x + 5
<syntaxhighlight lang="lua">script.Parent.Value = Vector3.new(x, y, z)</syntaxhighlight>
y = y + 5
Let's take a look at what we have so far for this method:
z = z + 5
<syntaxhighlight lang="lua">
</pre>
local value = script.Parent.Value
 
local x, y, z = value.x, value.y, value.z
Now we have the new x, y, and z. Let's take a look at what we have so far:
x, y, z = x + 5, y + 5, z + 5
 
script.Parent.Value = Vector3.new(x, y, z)
<pre>
</syntaxhighlight>
local value = {script.Parent.Value}
local i = 1
local x = value[i].x
local y = value[i].y
local z = value[i].z
x = x + 5
y = y + 5
z = z + 5
</pre>
 
The 3 single values have changed, but not the Vector3Value. In order to do that, we need to insert this line:
 
<pre>
script.Parent.Value = Vector3.new(x,y,z)
</pre>
 
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:
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:
 
<syntaxhighlight lang="lua">print(script.Parent.Value)</syntaxhighlight>
<pre>
print(script.Parent.Value)
</pre>
 
The script is complete! Let's just look at the finished script:
The script is complete! Let's just look at the finished script:
 
<syntaxhighlight lang="lua">
<pre>
local value = script.Parent.Value
local value = {script.Parent.Value}
local x, y, z = value.x, value.y, value.z
local i = 1
x, y, z = x + 5, y + 5, z + 5
local x = value[i].x
script.Parent.Value = Vector3.new(x, y, z)
local y = value[i].y
local z = value[i].z
x = x + 5
y = y + 5
z = z + 5
script.Parent.Value = Vector3.new(x,y,z)
print(script.Parent.Value)
print(script.Parent.Value)
</pre>
</syntaxhighlight>
 
Output should show <samp>20, 20, 20</samp>. Each time you run the script, the coordinates will change again.
Output should say 20, 20, 20.
===The Direct Method===
 
Usually there will be no need to keep track of all three individual coordinates. In this case, only one line is needed:
 
<syntaxhighlight lang="lua">script.Parent.Value = script.Parent.Value + Vector3.new(5, 5, 5)</syntaxhighlight>
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.
==Subtracting Vector3 Values==
==Subtracting Vector3 Values==
 
We've added them, but we now subtract them! Subtracting them is just like adding them, only we one line.  
 
<syntaxhighlight lang="lua">x, y, z = x - 5, y - 5, z - 5</syntaxhighlight>
We've added them, but we now subtract them! Subtracting them is just like adding them, only we edit 3 lines.  
 
<pre>
x = x - 5
y = y - 5
z = z - 5
</pre>
 
Now, let's take a look at that edit in the full script:
Now, let's take a look at that edit in the full script:
 
<syntaxhighlight lang="lua">
<pre>
local value = script.Parent.Value
local value = {script.Parent.Value}
local x, y, z = value.x, value.y, value.z
local i = 1
x, y, z = x - 5, y - 5, z - 5
local x = value[i].x
script.Parent.Value = Vector3.new(x, y, z)
local y = value[i].y
local z = value[i].z
x = x - 5
y = y - 5
z = z - 5
script.Parent.Value = Vector3.new(x,y,z)
print(script.Parent.Value)
print(script.Parent.Value)
</pre>
</syntaxhighlight>
 
Output should show <samp>10, 10, 10</samp>. You're done with subtracting them!
Output should say 10, 10, 10. You're done with subtracting them!
==Multiplying/Dividing Vector3 Values==
 
== Multiplying/Dividing Vector3 Values ==
 
This gets a bit more tricky to do.  Of course, you can always use the system used above, and get something like:
This gets a bit more tricky to do.  Of course, you can always use the system used above, and get something like:
 
<syntaxhighlight lang="lua">
<pre>
local value = script.Parent.Value
local value = {script.Parent.Value}
local x, y, z = value.x, value.y, value.z
local i = 1
x, y, z = x*2, y*2, z*2
local x = value[i].x
local y = value[i].y
local z = value[i].z
x = x * 2
y = y * 2
z = z * 2
script.Parent.Value = Vector3.new(x,y,z)
script.Parent.Value = Vector3.new(x,y,z)
print(script.Parent.Value)
print(script.Parent.Value)
</pre>
</syntaxhighlight>
 
...but sometimes one line is better to look at than 8.  So this time, we will multiply the {{type|Vector3}} value itself instead of its individual values.
...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:


<pre>
To do this, either multiply it by a number (x, y, and z will all be multiplied by this) or multiply it by another {{type|Vector3}} value (x, y, and z from your original {{type|Vector3}} will be multiplied with the respective number in the new {{type|Vector3}}).  A look at how this works:
local value = {script.Parent.Value}
<syntaxhighlight lang="lua">
local value = script.Parent.Value
print(value)  --Yes, you can print this way also.
print(value)  --Yes, you can print this way also.
value = script.Parent.Value * 2
script.Parent.Value = value * 2
print(script.Parent.Value)
print(script.Parent.Value)
</pre>
</syntaxhighlight>
 
This script multiplies everything in the {{type|Vector3}} value by the same number, which is 2 in this case.   
This script multiplies everything in the Vector3 value by the same number, which is 2 in this case.  For some reason, Roblox won't multiply the variable ("value") by the constant number (2); thus, it needs to be written out in the script.


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:
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:
 
<syntaxhighlight lang="lua">
<pre>
print(script.Parent.Value)
print(script.Parent.Value)
local value = {script.Parent.Value}
local value = script.Parent.Value
value = value * Vector3.new(2, 2, 0)
value = value * Vector3.new(2, 2, 0)
print(value)
print(value)
</pre>
</syntaxhighlight>
 
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 <var>value</var> by a {{type|Vector3}} and not have to worry about the script breaking.
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 *.
Dividing also works the same way.  Just use / instead of *.
 
<syntaxhighlight lang="lua">
<pre>
print(script.Parent.Value)
print(script.Parent.Value)
local value = {script.Parent.Value}
local value = script.Parent.Value
value = script.Parent.Value / 2
value = script.Parent.Value / 2
print(value)
print(value)
value = value / Vector3.new(2, 1, 0)
value = value / Vector3.new(2, 1, 0)
print(value)
print(value)
</pre>
</syntaxhighlight>
 
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.
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.  
 
== Custom Objects ==
A custom object is something that you create that has [[Properties|properties]] like the other ROBLOX objects, such as game.Workspace.Brick.Transparency
 
== Declaring an object ==
 
Your data type here will be a "Vector2"
<pre>
Vector2 = {} --this declares an object of the class "Vector2" (yes, it is a table as well)
 
function Vector2.new(one, two) --the function that we will use to create new objects of this class
newTB = {x, y} --this makes our new object with the properties, which you set as objects in the table
newTB.x = one --set the value to the one called in the argument
newTB.y = two
return newTB
end
</pre>
 
Now, we can call a new object of the class as such:
 
<pre>
a = Vector2.new(7, 8)
print(a.x) --> 7
print(a.y) --> 8
a.x = 10
print(a.x) --> 10
</pre>
 
== Adding member functions to your object ==
 
<pre>
function Vector2.new(one, two)
newTB = {x, y}
newTB.x = one
newTB.y = two
 
function newTB.reset()
newTB.x = 0
newTB.y = 0
end
 
return newTB
end
 
a = Vector2.new(5, 8)
 
print(a.x) --> 5
print(a.y) --> 8
 
a:reset()
 
print(a.x) --> 0
print(a.y) --> 0 </pre>


[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Latest revision as of 21:26, 28 April 2023

This is an easy, scripting related tutorial.

Introduction

This tutorial will show you how to add or subtract Vector3 values. This will be quite a benefit to your scripts, and will make them more popular if you have them.

Adding Vector3 Values

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

Scripting

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.

The Direct Method

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.

Subtracting Vector3 Values

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!

Multiplying/Dividing Vector3 Values

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.