Need Help with my code

I am making a placement system but I came across 2 problems.

I have made a Module Script in replicated storage called “PlacementValidator”

first problem is how can I set a max hight?

for example, I would like to place 3 objects each on top and after that, I can’t

Second problem how can I make it can’t collide with other objects?

I have no clue how to make it check if it’s colliding anything

https://streamable.com/n87ce3

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local modules = ReplicatedStorage:WaitForChild("Modules")
local gridSize = modules:WaitForChild("GridSize").Value

local placedProducts = Workspace:WaitForChild("PlacedProducts")
local whitelistedToPlace = Workspace:WaitForChild("WhitelistedToPlace")


local function isValid(cframe: CFrame, size: Vector3): boolean
	local overlapParams = OverlapParams.new()
	overlapParams.FilterDescendantsInstances = {placedProducts:GetChildren(), whitelistedToPlace:GetChildren()}
	overlapParams.FilterType = Enum.RaycastFilterType.Include

	if Workspace:GetPartBoundsInBox(cframe, size, overlapParams)[1] then
		return true
	else
		return false
	end
end

local function snapToGrid(position: Vector3): Vector3
	return Vector3.new(
		math.round(position.X / gridSize) * gridSize,
		position.Y,
		math.round(position.Z / gridSize) * gridSize)
end

local placementValidator = {
	IsValid = isValid,
	SnapToGrid = snapToGrid
}

return placementValidator

still not solved it

isn’t the isValid function making sure objects can’t collide and can’t you just add a limit on the Y axis to set a max height

that’s what I have tried to do, but it won’t work

how are using isValid and snapToGrid?

what do you mean?, is valid needs to return true if valid and false if not,

snap to grid is used when its valid and I am checking for valid space to place

I meant how ur calling the functions?

its been called from module script that maintain the placement system in replicated storage:

local function onRenderStepped()
	mousePosition = Get3dMousePosition(raycastParams)
	if snapToGrid then mousePosition = PlacementValidator.SnapToGrid(mousePosition) end

	local cframe = CFrame.new(mousePosition) * CFrame.fromEulerAnglesYXZ(0, math.rad(currentAngle), 0)
	local size = if itemClone:IsA("Model") then itemClone:GetExtentsSize() else itemClone.Size
	local isValid = PlacementValidator.IsValid(cframe, size)

	boxOutline.SurfaceColor3 = if isValid then VALID_COLOR else INVALID_COLOR
	itemClone:PivotTo(cframe)
end

@BrickBattlerOfTheDay

I don’t know why it’s not working, I hope you understand that. i need to disable the collision between objects. as you said I have tried include that folder “WhitelistedToPlace” and “PlacedProducts”

PlacedProducts is the folder when the object is placed in its his parent(workspace).
WhitelistedToPlace is the folder that includes the placeable objects like shelf or something in my case that floor platform

I don’t know why it’s not working, I hope you understand that. i need to disable the collision between objects. as you said I have tried include that folder “WhitelistedToPlace” and “PlacedProducts”

PlacedProducts is the folder when the object is placed in its his parent(workspace).
WhitelistedToPlace is the folder that includes the placeable objects like shelf or something in my case that floor platform

Does anyone know how to fix it? :pray:

still not solved i am stuck please if someone can help me :pray:

i’m unable to write code for you right now but I can hopefully push you in the right direction

for the height limit, since you want specifically 3 objects to be placed on one another regardless of size, whenever you place an object, detect if it’s on top of 3 or more other objects, and if so, cancel the function.

you can use the same logic for collision, using WorldRoot:GetPartsInPart() the server can check if the part is in another object’s hitbox.

didn’t work i tried, any other solutions?