Easy way to detect (somewhat) parts collision position

I just made this module script because there is no way (at least from my knowledge) detecting parts collision position



The code

local module = {}
module.__index = module

local DestroyAttOnEndCol = true

local function GetTouchingParts(part)
	local connection = part.Touched:Connect(function() end)
	local results = part:GetTouchingParts()
	connection:Disconnect()
	return results
end

function module.DetectPositionCollision(part1,part2)
	local IsCollsion = GetTouchingParts(part1)
	if IsCollsion == nil or #IsCollsion == 0 then if workspace.Terrain:FindFirstChild(part1.Name.."and"..part2.Name.."CP") and DestroyAttOnEndCol == true then workspace.Terrain:FindFirstChild(part1.Name.."and"..part2.Name.."CP"):Destroy() end return false else
		if table.find(IsCollsion,part2,1) then
			local att
			if workspace.Terrain:FindFirstChild(part1.Name.."and"..part2.Name.."CP") then
				att = workspace.Terrain:FindFirstChild(part1.Name.."and"..part2.Name.."CP")
			else
				att = Instance.new("Attachment",workspace.Terrain)
				att.Name = part1.Name.."and"..part2.Name.."CP"
				att.Visible = true
			end
			
			local newunion2 
			local newUnion
			local cl1
			local cl21
			local cl22
			
			if part1.Size.magnitude > part2.Size.magnitude then
				cl1 = part1:Clone()
				cl1.Parent = workspace.Terrain
				cl1.Transparency = 1
				cl1.Locked = true
				cl1.Size = part1.Size
				cl21 = part2:Clone()
				cl21.Parent = workspace.Terrain
				cl21.Transparency = 1
				cl21.Locked = true
				cl21.Size = part2.Size
				cl22 = part2:Clone()
				cl22.Parent = workspace.Terrain
				cl22.Transparency = 1
				cl22.Locked = true
				cl22.Size = part2.Size - Vector3.new(0.01,0.01,0.01)

				local otherParts = {cl1}
				newUnion = cl21:SubtractAsync(otherParts)
				newUnion.Parent = workspace.Terrain
				newUnion.Transparency = 1
				newUnion.Locked = true

				local otherParts2 = {newUnion}
				newunion2 = cl22:SubtractAsync(otherParts2)
				newunion2.Parent = workspace.Terrain
				newunion2.Transparency = 1
				newunion2.Locked = true	
			else if part2.Size.magnitude > part1.Size.magnitude then
					cl1 = part1:Clone()
					cl1.Parent = workspace.Terrain
					cl1.Transparency = 1
					cl1.Locked = true
					cl1.Size = part1.Size
					cl21 = part2:Clone()
					cl21.Parent = workspace.Terrain
					cl21.Transparency = 1
					cl21.Locked = true
					cl21.Size = part2.Size
					cl22 = part2:Clone()
					cl22.Parent = workspace.Terrain
					cl22.Transparency = 1
					cl22.Locked = true
					cl22.Size = part2.Size - Vector3.new(0.01,0.01,0.01)

					local otherParts = {cl1}
					newUnion = cl21:SubtractAsync(otherParts)
					newUnion.Parent = workspace.Terrain
					newUnion.Transparency = 1
					newUnion.Locked = true

					local otherParts2 = {newUnion}
					newunion2 = cl22:SubtractAsync(otherParts2)
					newunion2.Parent = workspace.Terrain
					newunion2.Transparency = 1
					newunion2.Locked = true	
				else
					cl1 = part1:Clone()
					cl1.Parent = workspace.Terrain
					cl1.Transparency = 1
					cl1.Locked = true
					cl1.Size = part1.Size
					cl21 = part2:Clone()
					cl21.Parent = workspace.Terrain
					cl21.Transparency = 1
					cl21.Locked = true
					cl21.Size = part2.Size
					cl22 = part2:Clone()
					cl22.Parent = workspace.Terrain
					cl22.Transparency = 1
					cl22.Locked = true
					cl22.Size = part2.Size - Vector3.new(0.01,0.01,0.01)

					local otherParts = {cl1}
					newUnion = cl21:SubtractAsync(otherParts)
					newUnion.Parent = workspace.Terrain
					newUnion.Transparency = 1
					newUnion.Locked = true

					local otherParts2 = {newUnion}
					newunion2 = cl22:SubtractAsync(otherParts2)
					newunion2.Parent = workspace.Terrain
					newunion2.Transparency = 1
					newunion2.Locked = true	
				end
			end
			
			att.WorldPosition = newunion2.Position
			cl1:Destroy()
			cl21:Destroy()
			cl22:Destroy()
			newunion2:Destroy()
			newUnion:Destroy()
			return att.WorldPosition,att
		else
			if workspace.Terrain:FindFirstChild(part1.Name.."and"..part2.Name.."CP") and DestroyAttOnEndCol == true then workspace.Terrain:FindFirstChild(part1.Name.."and"..part2.Name.."CP"):Destroy() end
			return false
		end
	end
end

return module

and here is a test project

PositionCollisionDetection.rbxl (50.6 KB)

and the module

https://www.roblox.com/library/8018252686/PositionCollision

7 Likes

In studio, or are you talking about in game?
If it’s in Studio there is a tool in the Settings>Physics menu: PhysicsSettings | Roblox Creator Documentation.
that shows a coloured sphere where Parts touch and it also works in Test mode in Studio.

1 Like

it works in game too

u can use it like to display particles (on hit events)
or weld things better

also thanks for this useful information

1 Like

There was a fatal mistake in my script which caused not accurate detection

Now its updated and working

If u got the module and used it make sure to update it

1 Like