Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why the mathematical operation isn't working?

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?

2 comments

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

0

Thanks.

2 answers

✓ Accepted answer

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

1 comment

0

Thanks.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.