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

How do you get the position of a part without viewing the Workspace tree?

Asked by 9 years ago

Hello,

I am working on a script involving terrain generation and am creating new instances using clones from an original object. The problem is, I am viewing the Workspace to copy values, particularly the position and size into my script.

The original object in the Workspace, DirtBlock, is in the coordinates listed below for x, y, and z, respectively. it is exactly a 3 by 3 by 3 size block.

Here is the code [I would like to avoid]:

--the coordinates of the original DirtBlock
x= -98.5
y= 2002
z= 98.5

clone= game.Workspace.DirtBlock:Clone()
clone.Name= "DirtBlockClone"
clone.Parent= game.Workspace
clone.Position= Vector3.new(-101.5,2002,98.5) -- as you can see it is moved 3 studs in the x direction

My attempt is to use something like: bob= game.Workspace.DirtBlock:getInformation() and then print out the variable bob.

So, if i have the x, y, and z coordinates, I can do something, theoretically speaking, like this:

xNew= game.Workspace.DirtBlock.Position(x)
yNew= game.Workspace.DirtBlock.Position(y)
zNew= game.Workspace.DirtBlock.Position(z)
clone.Position=Vector3.new(xNew,yNew,zNew)

Edit: I'm not sure how I will manage this, as I have tried approaches like creating instances from scratch with identically similar descendant properties.

0
I'm not sure if I misread your goal, but couldn't you just get the position of a block by using .Position.X(or Y or Z)? RoboFrog 400 — 9y
0
Yes, the code worked out; thank you for your assistance. I was confused because I was inputting the wrong format for the position attribute, in addition to using the Instance.new() method. Are there other ways to approach this? Houlardy642 28 — 9y

2 answers

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

It tends to be a good idea to group related information into a single value. Data structures like Vector3 let us do that.

Well designed objects let us do things to them without us tearing them apart and putting them together. For example, Vector3 let's us use +:

local right = Vector3.new(0, 0, 3)

local original = Vector3.new(-98.5, 2002, 98.5)

local newPosition = original + right
-- 3 studs in the z direction from `original`
0
So, it is like how the local variable "right" determines by how much something moves, when paired up with the original object. Thank you for your post. I will have to play around with Vector3. Houlardy642 28 — 9y
0
Yup. Vectors mean change in position, as well as position itself. BlueTaslem 18071 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

RoboFrog, you mean as in : print(game.Workspace.DirtBlock.Position.x)?

Or, testx= game.Workspace.DirtBlock.Position.x and then substitute that in the Vector3 array for the x coordinate?

0
Your best bet is the second option, which will function much like a variable. BlueTaslem's answer goes into extra detail and is definitely worth reading over. RoboFrog 400 — 9y
0
I see; I'm open to multiple approaches, as in some cases in programming in other languages (my personal experience), some methods work out better over another depending on the circumstances involved. Houlardy642 28 — 9y

Answer this question