How would i not make parts overlap when trying to place them?

something about a grid system and trying to place parts with the mouse, i try place parts and they overlap by when trying to place on the side of each other or too close

heres the current code

-- Varibles
local Player = game:GetService("Players").LocalPlayer
local RunService = game:GetService("RunService")
local Ts = game:GetService("TweenService")

local Mouse = Player:GetMouse()

local PlotModule = require(game.ReplicatedStorage.Modules.plotHandler)

local ServerPlaceEvent = game.ReplicatedStorage.Events.PlaceEvent
local PlaceEvent = game.ReplicatedStorage.Events.StartPlace

local PlacementPart = game.ReplicatedStorage.Items.AreaPart:Clone(); PlacementPart.Size = Vector3.new(1,1,1)

local CurrentPart
local v	
local Pos
local LPos

local GridSize = .5

-- Functions
local function RVector(Pos)
	return Vector3.new((Pos.X - Pos.X % GridSize), (Pos.Y - Pos.Y % GridSize), (Pos.Z - Pos.Z % GridSize))
end

local function MovePart()
	local PSize = PlacementPart.Size
	Pos = RVector(Mouse.Hit.p) + Vector3.new(0,PSize.Y/2,0)
	
	PlacementPart.Position = Pos
end

local function ReadyPlacing(PartSize)
	if CurrentPart and PartSize == CurrentPart then
		RunService:UnbindFromRenderStep("MovePart")
		PlacementPart.Parent = game.ReplicatedStorage.Items
		CurrentPart = nil
		return
	end
	
	if CurrentPart and PartSize ~= CurrentPart then
		RunService:UnbindFromRenderStep("MovePart")
	end
	
	CurrentPart = PartSize
	
	if PartSize == "1x1" then
		v = Vector3.new(1,1,1)
	elseif PartSize == "2x2" then
		v = Vector3.new(2,2,2)
	end
	
	PlacementPart.Parent = workspace
	PlacementPart.Size = v
	
	RunService:BindToRenderStep("MovePart", 1, MovePart)
end

local function PlacePart()
	if CurrentPart then
		ServerPlaceEvent:FireServer(v, Pos)
	end
end

-- Main

-- Event Functions
PlaceEvent.Event:Connect(ReadyPlacing)
Mouse.Button1Down:Connect(PlacePart)

(edit)
heres a video of it happening
& updated some of the code

1 Like