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

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

Asked by
LawlR 182
5 years ago
Edited 5 years ago

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

Log in to vote
1
Answered by 5 years ago

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 :\

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). LawlR 182 — 5y
0
Here is what happens in your solution: https://gyazo.com/bbacaeedf2c3b64206ce9f62c7d01b0c LawlR 182 — 5y
0
sorry for the late response, but DO NOT USE POSITION, it has auto collision detection, use CFrame fanofpixels 718 — 5y
Ad

Answer this question