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

How to perform math on a parts position?

Asked by 5 years ago
Edited 5 years ago

The first thing I tried was

part.Position.X = math.floor(part.Position/3+0.5)*3

but then I did some research and realized you cant do that. but all pages I could find on this weren't on performing math on it.

now I'm stuck because even if I wanted to slide it with vector3 I dont know how I would do a math.floor with that

Output: (Vector3 expected, got number)

and lol sorry asking a lot of questions today, but they've all been really helpful


Edit:

I'm trying to do this with XY&Z so I can use that equation above to round a parts position to nearest interval of 3

Edit: This is in a while true loop going to the players mouse and then applying this to make a grid placing system of every 3 pixels in each direction

0
you can't change the x value like that HappyTimIsHim 652 — 5y

2 answers

Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

Sub-properties under the parent Properties like:

  • BasePart.Position.X
  • BasePart.BrickColor.r

or even:

  • BasePart.CFrame.Position

are read-only, or immutable, properties. They are properties that can't be changed by setting it to some int/number. You need to directly set the parent property with the required data type.

So setting Position would require a Vector3:

BasePart.Position = Vector3.new(0,0,0)

--sets basepart to coordinate 0,0,0

Performing 'math' on the current position of the part could be done by using the addition, +, operator as an example:

BasePart.Position = BasePart.Position + Vector3.new(0, 10, 0)

--would move the part's current position 10 studs up

There is so much more you could do with positioning in CFrames that can also include rotational math. It's a beast of it's own but is worth learning.

CFrame

Let me know if I missed anything or you need something explained.

Edit:

Calculate position values beforehand then construct a new Vector3 with those values in it:

local offset = 10

local random = math.random(1, offset)  --would get a number between 1-10

local vector3 = Vector3.new(0, random, 0)

BasePart.Position = BasePart.Position + vector3

This is an example. This only runs the code once until you run it again so it should move the part upwards depending on the random number returned on line 03.

0
how would I use a math.floor on it like used in the question with all those equations? barrettr500 53 — 5y
0
You'll need to provide what you're looking to do exactly so I can further help if possible. If not, I've edited my answer and included a similar case to how you would use math.floor or math.random xPolarium 1388 — 5y
0
Ok hopefully its easier to understand now barrettr500 53 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
--instead of using the vector3 values, i'd use CFrame.new
part.CFrame = part.CFrame + Vector3.new(0,10,0)

Answer this question