How to get all sides of a basepart

Hello, I’m trying to get all the sides of a part, and put a part there, but I can’t even get all the sides becaues I don’t know how too, heres part of the script.

local function on_part_click()
	
	local parts_sides = {-part_size.X / 2, part_size.X / 2, -part_size.Y / 2, part_size.Y /2, -part_size.Z / 2, part_size.Z / 2}
	
	for i,v in pairs(parts_sides) do
		local new_part = first_part:Clone()
		print(i)
	end
	
	
end

We can probably achieve this with an easier method, but what are you trying to do? Clone the part and place it in front of the sides?

Kinda? it’s for a mining system, I used to know how to do this, but I forgot, and I want to do this without AI and become a better developer, but yes, I want to place a part on every side of the first_part.

1 Like

Alright well getting the sides might be overdoing it so instead we can use directional vectors!

So if you want to place a part in the front, we can just take the reference part and use the LookVector (what way the part is facing) and multiply it by some number (like 2 or 3) and it’ll be in front!

Alright now what about if we wanna place a part in the front?

Well, we don’t have a “behind vector” property so we can just use -LookVector which is the same thing basically.

If our part has a LookVector of (0, 0, -1) (facing forward relative to the origin)

then our “behind vector” would be (0, 0, 1) aka the opposite direction!

Hopefully this has been so far, we can also work with the RightVector property to place a part on the right side.

If we want the left, we can just negate the RightVector property.

And there is also an UpVector property. Hopefully you can now figure out how to get the down vector


One last thing…

You have to add the result of the directional vector to the reference part’s position.

“Why?”

Well, just changing the part’s position to the LookVector or whatever (0, 0, -1 for example) would just place it in the wrong place. By adding the value to the position, we’re basically moving a little in that direction if that makes sense.

If our part is at
(10, 10, 10) and we wanna move forward (assuming the part is facing forward relative to the origin)…

Then our new position would be
(10, 10, 9) since forwards would be (0, 0, -1)!

In code, you could basically write something like this I guess:

-- example ofc
local part = …
local Newpart = …
local place_to_put =
   Vector3.new(
      part.Position
      +
      part.CFrame.LookVector * 3
   )

Newpart.Position = place_to_put

Lmk if there’s anything confusing / mistakes so I can fix my post!

3 Likes

That’s pretty helpful, I got the sides thing working, I probably did it the wrong way, but now the parts won’t spawn in a other area.

local function on_part_click(current_part)
	
	local part_cframe = current_part.CFrame
	local part_size = current_part.Size
	
	local parts_sides = {
		part_cframe.LookVector * part_size.Z / 2 * 2,
		part_cframe.LookVector * -part_size.Z / 2 * 2,
		part_cframe.RightVector * part_size.X / 2 * 2,
		part_cframe.RightVector * -part_size.X / 2 * 2,
		part_cframe.UpVector * part_size.Y / 2 * 2,
		part_cframe.UpVector * -part_size.Y / 2 * 2
	}
	
	for _, v in pairs(parts_sides) do
		
		if not table.find(mined_table, v) then
		
		local new_part = first_part:Clone()
		new_part.Position = first_part.Position + v
		new_part.Parent = work_space
			
		new_part.ClickDetector.MouseClick:Connect(function()
			on_part_click(new_part)
			new_part:Destroy()
		end)
		
		table.insert(mined_table, v)
			
		end
	end
	
	
end

first_part.ClickDetector.MouseClick:Connect(function()
	on_part_click(first_part)
	first_part:Destroy()
end)

Note: I do not use tables very much, so they are very new to me.

1 Like

Do you know what part of your code is causing this? I just skimmed thru a bit and don’t know which part is not rigth

1 Like

I’d say here. I’m not 100% sure.

		if not table.find(mined_table, v) then
		
		local new_part = first_part:Clone()
		new_part.Position = first_part.Position + v
		new_part.Parent = work_space
			
		new_part.ClickDetector.MouseClick:Connect(function()
			on_part_click(new_part)
			new_part:Destroy()
		end)
		
		table.insert(mined_table, v)
			
		end

what I think it’s doing is well I want it do put the position of the parts that spawned, so it can’t spawn other parts in the same area, then spawn other parts around that part that was clicked.

1 Like

Alright so I read thru it and yeah I understand it now. So like when you click one of the new parts being spawned does it not work? Or is it fi you click an unrelated part (not spawned in from your function here)?

Unless idk what you meant here

1 Like

I’m not very good at this, but heres the closet I got.

local function on_part_click(current_part)
	
	local part_cframe = current_part.CFrame
	local part_size = current_part.Size
	
	local parts_sides = {
		part_cframe.LookVector * part_size.Z / 2 * 2,
		part_cframe.LookVector * -part_size.Z / 2 * 2,
		part_cframe.RightVector * part_size.X / 2 * 2,
		part_cframe.RightVector * -part_size.X / 2 * 2,
		part_cframe.UpVector * part_size.Y / 2 * 2,
		part_cframe.UpVector * -part_size.Y / 2 * 2
	}
	
	for _, v in pairs(parts_sides) do
	
		if not table.find(mined_table, v) then
		
		local new_part
		
		if first_part then
			new_part = first_part:Clone()
			new_part.Position = current_part.Position + v
			new_part.Parent = work_space
		else
			new_part = current_part:Clone()
			new_part.Position = current_part.Position + v
			new_part.Parent = work_space
		end
		
		
		if new_part:FindFirstChild("ClickDetector") then
				
			new_part.ClickDetector.MouseClick:Connect(function()
				on_part_click(new_part)
				new_part:Destroy()
			end)
				
		else
				
			local click_detector = Instance.new("ClickDetector")
			click_detector.Parent = new_part
				
			click_detector.MouseClick:Connect(function()
				on_part_click(new_part)
			end)
				
				table.insert(mined_table, v)
				
			end	
		end	
	end
end
1 Like

Did it work now? Or are you still having that problem you were talking about earlier??

1 Like

It’s closer, but it’s spawning parts at the block I clicked, like you know how in a mining game, you can mine, and it’ll spawn blocks around that part you just mined, and it won’t put a part where the part you just mined, but my is putting blocks where you just mined.

1 Like

Maybe you could show a video cuz I’m still a little confused

1 Like

when I opened up that area and clicked the blocks on top, those shouldn’t have spawned, and it should’ve been no blocks in there.

it should’ve been like a cave

I hope this helps clear everything.

1 Like

For this one, it kinda seems like when you spawn more of those parts, the parts being place in the front are forming that second layer so it won’t be hollow (a cave) I guess.


I see here

You insert one of the sides that has been used to see if you already put a part there or not but I guess it’s not working.

I’m not sure why this doesn’t work yet but I’ll figure it out soon

1 Like

Here’s an updated one, that is almost there.

local mined_table = {}

local function on_part_click(current_part)
	
	local part_cframe = current_part.CFrame
	local part_size = current_part.Size
	
	local parts_sides = {
		part_cframe.LookVector * part_size.Z / 2 * 2,
		part_cframe.LookVector * -part_size.Z / 2 * 2,
		part_cframe.RightVector * part_size.X / 2 * 2,
		part_cframe.RightVector * -part_size.X / 2 * 2,
		part_cframe.UpVector * part_size.Y / 2 * 2,
		part_cframe.UpVector * -part_size.Y / 2 * 2
	}
	
	for _, v in pairs(parts_sides) do
		
		if not mined_table[v] then
		
		local new_part = current_part:Clone()
		new_part.Position = current_part.Position + v
		new_part.Parent = work_space
		
		if new_part:FindFirstChild("ClickDetector") then
				
			new_part.ClickDetector.MouseClick:Connect(function()
				on_part_click(new_part)
				mined_table[v] = true
				new_part:Destroy()
			end)
				
			else
				
			local click_detector = Instance.new("ClickDetector")
			click_detector.Parent = new_part
				
			click_detector.MouseClick:Connect(function()
				on_part_click(new_part)
				mined_table[v] = true
				new_part:Destroy()
			end)
				
			end
			
		end	
		
	end
end

first_part.ClickDetector.MouseClick:Connect(function()
	on_part_click(first_part)
	first_part:Destroy()
	mined_table[first_part.Position] = true
end)
1 Like

So is it still possible to mine parts in that second layer and it spawns parts where it ain’t supposed to?

1 Like

1 Like

Oh so it works now? Nice! Glad that you were able to figure it out on your own!

1 Like

Not really, it works for the first, in the middle/where the first block was. but if you keep going it just breaks, I found out that the part_sizes is not updating and just staying at where the first part was.

Even then, if you start it, and click the sides it just does this.
image
the middle part isn’t supped to be there.

1 Like