Scripting Help in Roblox Studio

Hello everyone! I am trying to make a simple building blocks script in Roblox Studio, although I made it I have found that the part which is a display before its placed can even be placed in the air. And I want it always to be on the surface of baseplate.Does anyone know how to do that?

Here is the script:

local unitRay = mouse.UnitRay
local origin = mouse.Origin
local part = game.ReplicatedStorage.Part:Clone()

mouse.Move:Connect(function()
    part.Parent = workspace
    part.CFrame = mouse.Hit
    part.Orientation = Vector3.new(0,0,0)
    part.Transparency = 0.5
    part.Name = "notplacedpart"
    part.Anchored = true
    part.CanCollide = false
    print(mouse.Hit)
end)

mouse.Button1Down:Connect(function()
    local part = Instance.new("Part",workspace)
    part.CFrame = game.Workspace:WaitForChild("notplacedpart").CFrame
    part.Orientation = Vector3.new(0,0,0)
    part.Anchored = true
end)```

And I also want it to not be more than 15 studs away from the player or you cannot place the block.

Does it work if we do like this?

local unitRay = mouse.UnitRay
local origin = mouse.Origin
local part = game.ReplicatedStorage.Part:Clone()

mouse.Move:Connect(function()
    part.Parent = workspace
    part.CFrame = mouse.Hit
    part.Orientation = Vector3.new(0,0,0)
    part.Transparency = 0.5
    part.Name = "notplacedpart"
    part.Anchored = true
    part.CanCollide = false
    while wait() do
        part.Position = Vector3.new(part.Position.X,0.5,part.Position.Z)
    end
end)

mouse.Button1Down:Connect(function()
    local part = Instance.new("Part",workspace)
    part.CFrame = game.Workspace:WaitForChild("notplacedpart").CFrame
    part.Orientation = Vector3.new(0,0,0)
    part.Anchored = true
end)

Hey there.

You would need to set the TargetFilter property of the mouse to the part you’re placing, so that mouse.Hit isn’t the part itself.

mouse.TargetFilter = part
1 Like