Help about structure placement!

I wanna achive if part inside of a wall it should return to previous spot that it was not in wall

Example:
My part is here
image

I drag my part close to wall
image

I dragged my part inside of the wall
image

And when i try to drag inside of the wall or atleast mouse cursor i wanna return to or stick to this position
image

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local LocalInput = require(script.Parent.Parent.LocalInput)
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()

local Model = ReplicatedStorage:WaitForChild("Sellables").Structures.Part.Model

local gridSize = 0.5 

--------------------------[[ Functions ]]--------------------------

function PrepareModel()
	local NewModel = Model:Clone()
	NewModel.Parent = workspace

	local Highlight = Instance.new("Highlight", NewModel)
	Highlight.Adornee = NewModel
	Highlight.Name = "StructureHighlight"

	for int, parts in ipairs(NewModel:GetChildren()) do
		if parts:IsA("Part") then
			parts.CanCollide = false
			parts.CanTouch = false
			parts.CanQuery = false
		end
	end

	return NewModel
end

function ConvertToGridSize(mousePosition, HeightSize)
	local newX = math.round(mousePosition.X / gridSize) * gridSize
	local newZ = math.round(mousePosition.Z / gridSize) * gridSize
	local newY = math.round(mousePosition.Y / gridSize) * gridSize
	local newCframe = CFrame.new(newX, newY + (HeightSize / 2), newZ)
	return newCframe
end

function MoveStructure()
	local NewModel = PrepareModel()
	local modelHeight = NewModel.PrimaryPart.Size.Y 
	
	while true do
		local mousePosition = Mouse.Hit.Position
		local NewCFrame = ConvertToGridSize(mousePosition, modelHeight)
		NewModel:SetPrimaryPartCFrame(NewCFrame)
		RunService.RenderStepped:Wait()
	end
end

--------------------------[[ Inputs ]]--------------------------

LocalInput.moveStructure(MoveStructure)

How i do that? I don’t think with raycasts because i wanna place blocks top of it too!

2 Likes

use workspace:GetPartsBoundInBox and subtract a little bit from the bounding size so you can place it directly next to other parts

also include an overlap param only including the map and other parts so you can save performance

you can also use bounding box or extents size to get the bounding size of a model

use these inside the loop to check if the location overlaps with another part
if the position is valid, then update the position of the model

2 Likes