How To Increase and Decrease Vector3 Values

From Legacy Roblox Wiki
Revision as of 10:14, 26 August 2008 by >Mindraker (handholding)
Jump to navigationJump to search

Introduction

This tutorial will show you how to add or subtract 3 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.

local value = {script.Parent.Value}

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.

local i = 1

Let's look at our progress:

local value = {script.Parent.Value}
local i = 1

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...

local x = value[i].x
local y = value[i].y
local z = value[i].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 = x + 5
y = y + 5
z = z + 5

Now we have the new x, y, and z. Let's take a look at what we have so far:

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

The 3 single values have changed, but not the Vector3Value. In order to do that, we need to insert this line:

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 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
script.Parent.Value = Vector3.new(x,y,z)
print(script.Parent.Value)

Output should say 20, 20, 20.


Subtracting Vector3 Values

We've added them, but we now subtract them! Subtracting them is just like adding them, only we edit 3 lines.

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

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

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
script.Parent.Value = Vector3.new(x,y,z)
print(script.Parent.Value)

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