Destroy isnt working (Read Comments For updated post)

I am making a build system and i need to detect if the part is touching another part of else destroy it i cant do .touched because it has to be created first then use .touched but i want it to detect if it touches another part first, it needs to be able to detect blocks that are diagonal, i was thinking of raycasts but i was woundersing is there a more efficient method rather than having 5 raycats per side of a square

3 Likes
3 Likes

maybe use Part:GetTouchingParts()

2 Likes

i have put this

local Block = game.ReplicatedStorage.Blocks.Block:Clone()

	Block.Parent = game.Workspace
	Block.Parent = workspace
	Block.Position = SnapHRP
	Block.Size = Vector3.new(GridSnap, GridSnap, GridSnap)

	game.ReplicatedStorage.Build:FireServer(SnapHRP)
	
	local overlapParams = OverlapParams.new()
	OverlapParams.FilterDescendantsInstances = Block
	OverlapParams.FilterType = Enum.RaycastFilterType.Exclude
	
	local hitContents = workspace:GetPartBoundsInBox(SnapHRP, Vector3.new(GridSnap + 5, GridSnap + 5, GridSnap + 5), OverlapParams)
	
	for i, v in pairs(hitContents) do
		print(v)
	end
	

it isnt giving me any results

1 Like

I am assuming Floor is a model with :GetChildren() but it would be better to remove the GetChildren and just add it to the filter list.

local Block = game.ReplicatedStorage.Blocks.Block:Clone()

	Block.Parent = game.Workspace
	Block.Parent = workspace
	Block.Position = SnapHRP
	Block.Size = Vector3.new(GridSnap, GridSnap, GridSnap)

	game.ReplicatedStorage.Build:FireServer(SnapHRP)
	
	local IgnoreList = {}
	table.insert(IgnoreList, Block)
	table.insert(IgnoreList,Floor)
	
	
	local overlapParams = OverlapParams.new()
	overlapParams.FilterDescendantsInstances = IgnoreList
	overlapParams.FilterType = Enum.RaycastFilterType.Exclude
	
	local hitContents = workspace:GetPartBoundsInBox(Block.CFrame, Block.HitBox.Size, overlapParams)
	
	local IsBlock = true
	
	for i, HitParts in pairs(hitContents) do
		if HitParts.Name ~= "Block" then
			IsBlock = false
			break
		end
	end
	print(IsBlock)
	
	if not IsBlock then
		wait(0.05)
		Block:Destroy()
	end
3 Likes