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

How do you set the position of the top of a part?

I'm trying to make a placement system, however I'm not sure how to place the top of the part on the top of another part. I want it placed on top because if it placed the middle somewhere then there would be a huge block coming out of the ground. Here is my script which works, but it places the middle of the part instead of the top.

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Click = script.Parent

Click.MouseButton1Down:Connect(function()
	local Placed = false
	local Cancelled = false
	Mouse.Button1Down:Connect(function()
		Placed = true
	end)
	UserInputService.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.Q then
			Cancelled = true
		end
	end)
	local Baseplate = Instance.new("Part")
	Baseplate.Size = Vector3.new(150, 10, 150)
	Baseplate.Anchored = true
	Baseplate.Parent = workspace
	repeat
		wait()
		if Mouse.TargetFilter ~= Baseplate then
			Mouse.TargetFilter = Baseplate
		end
		local Character = Player.Character
		local MousePosition = Mouse.Hit.p
		if Mouse.Target ~= Baseplate and Mouse.Target then
			if Mouse.Target:IsA("Terrain") then
				Baseplate.CFrame = CFrame.new(MousePosition.X, Mouse.Target.Size.Y - Mouse.Target.Size.Y*0.9, MousePosition.Z)
			end
		end
	until Placed == true or Cancelled == true
	if Cancelled == true then
		Baseplate:Destroy()
	elseif Placed == true then
		script.Parent.Parent.Parent:Destroy()
	end
end)

1 answer

if the 2 parts never rotate, then its simple arithmetic

local PartToSpawn = Instance.new("Part", workspace)
--modify part properties and whatnot

local Part2 = workspace.Despacito -- part to spawn ontop of

local Y = Part2.Position.Y + (Part2.Size.Y/2) + (PartToSpawn.Size.Y/2)-- final result

if only the part to teleport to is rotating is a little more complicated

local PartToSpawn = Instance.new("Part", workspace)
--modify part properties and whatnot

local Part2 = workspace.Despacito -- part to spawn ontop of

local CF = Part2.CFrame * (Part2.CFrame.topVector * Part2.Size.Y)-- final result

if both rotate its too complicated for me :\

3 comments

0

This spawns the part on top of the other part. What I wanted is the top of the part that you are spawning to position on the top of the other part (the part that the mouse is pointing to).

0

Here is what happens in your solution: https://gyazo.com/bbacaeedf2c3b64206ce9f62c7d01b0c

0

sorry for the late response, but DO NOT USE POSITION, it has auto collision detection, use CFrame

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