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

Why the mathematical operation isn't working?

Asked by 5 years ago

All i want to do a script that get Parent positions (XYZ) and subtract it by a variable. Here's the script and error:

Script:

local PositionGoal = {-301.103, 153.029, -497.664}
local PositionStart = {script.Parent.Position}


local XPosition = PositionGoal[1]
local YPosition = PositionGoal[2]
local ZPosition = PositionGoal[3]

local XGoal = PositionStart[1]
local YGoal = PositionStart[2]
local ZGoal = PositionStart[3]

local MOVEMENT_GOAL = {(XPosition - XGoal), (YPosition - YGoal), (ZPosition - ZGoal)}

Error:

"Workspace.Part.Script:13: bad argument #1 to '?' (Vector3 expected, got number)"

From what i understand, it says that the operation is being expected from vector3. How can i fix that?

0
You're trying to subtract a number by a Vector3 value. You can only perform arithmetic operations on a Vector3 value with another Vector3 value and in some cases a CFrame value too. Read more here: https://developer.roblox.com/api-reference/datatype/Vector3 FIamezR 15 — 5y
0
Thanks. User#27525 1 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

As FlamezR has said, you can't subtract ordinary numbers from Vector3s, if you want to subtract the same amount from x, y, and z then you can simply do myVector3 - Vector3.new(num, num, num). Your code is also unnecessarily long, so I'll simplify it for you.

local PositionGoal = Vector3.new(-301.103, 153.029, -497.664)
local PositionStart = script.Parent.Position

local MOVEMENT_GOAL = PositionStart - PositionGoal
0
Thanks. User#27525 1 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

First, you are subtracting a Vector3 off a number, and there is only 1 value in PositionStart, if you're trying to get the X, Y or Z then do PositionStart[1].X etc

Answer this question