How to place block in the side of a block

Hello,

How do I make so that the 4x4x4 block is placed at the side of another block, the problem I’m getting here is that it’s placing inside a block.

Here is a demonstration of what I wanted to achieve:

My first approach was to use mouse.hit and detect the position, but it didn’t go well since the blocks is not snapping to the side.

2 Likes

You can do this relatively easily with raycasting. I wrote a response to someone about a week ago but I can’t find it rn.

Basically what you’d do is cast a ray from the mouse and use the normal returned from the ray to find the block’s “goal” position:

Here’s a very bare-bones block placement system:

local players = game:GetService('Players')
local userInputService = game:GetService('UserInputService')

local block = workspace.Block
local displayPart = block:Clone()
displayPart.Parent = workspace
displayPart.CanCollide = false
displayPart.Transparency = 0.5

local localPlayer = players.LocalPlayer
local mouse = localPlayer:GetMouse()

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {localPlayer.Character; displayPart}

local function updateDisplayBlock()
	local mouseUnitRay = mouse.UnitRay
	local ray = workspace:Raycast(mouseUnitRay.Origin, mouseUnitRay.Direction * 1000,  raycastParams)
	if ray then
		local normal: Vector3 = ray.Normal -- this returns the mouse hit's face
		local hitPart: BasePart = ray.Instance -- this is the part that our mouse is touching
		local displayPosition = hitPart.Position + normal * displayPart.Size -- this is where the magic happens
		displayPart.Position = displayPosition
	end
end

userInputService.InputBegan:Connect(function(inputObject: InputObject, gameProcessedEvent)
	if gameProcessedEvent then
		return
	end
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		local newDisplayPart = displayPart:Clone()
		newDisplayPart.Parent = workspace
		newDisplayPart.Transparency = 0
		newDisplayPart.CanCollide = true
		updateDisplayBlock()
	end
end)

mouse.Move:Connect(updateDisplayBlock)
2 Likes

Thank you for fast reply, I’ll try this tomorrow and give some updates!

1 Like

I tried it and it work! Thank you so much for helping me.

1 Like