How do i fix parts clipping into other parts?

Regarding the game im developing right now with a dragging system i scripted myself. The system itself works fine, but when the player lets go, the parts are supposed to stick to the nearest surface, but when doing so they can also clip in the nearest surface.

If i didn’t explain it clearly, here is a video:
robloxapp-20241102-0858276.wmv (2.9 MB)
And here are the two scripts responsible for the dragging system:
local script:

-- var defining 1

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local RS = game:GetService("ReplicatedStorage")

-- var defining 2

local selBlock
local dragBall

-- var defining 3

local dragging = false

-- inputbegan block (what happens when the player is attempting to drag a block)

UIS.InputBegan:Connect(function(input)
	if mouse.Button1Down and input.UserInputType == Enum.UserInputType.MouseButton1 and mouse.Target ~= nil and mouse.Target:HasTag("draggable") then
		selBlock = mouse.Target
		print(mouse.Target)
		if mouse.Target:IsA("BasePart") or mouse.Target:IsA("Mesh") or mouse.Target:IsA("UnionOperation") then
			dragging = true
			mouse.TargetFilter = selBlock
			-- what happens when the block is being dragged
			-- visuals set up in server script
			RS.VisualSet:FireServer(selBlock)
			while dragging == true do
				task.wait()
				local ASHV = mouse.Hit
				RS.ReplicateHit:FireServer(ASHV)
			end
		end
	end
end)

--inputended block (what happens when the player is no longer dragging a block)

UIS.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and mouse.Button1Up and dragging == true then
		dragging = false
		mouse.TargetFilter = nil
		selBlock = nil
	end
end)

--rotation block

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		RS.RotationHit:FireServer()
		print("rotate fired")
	end
end)

server script:

local dragBall
local selBlock2

local orient

local RS = game.ReplicatedStorage

RS.VisualSet.OnServerEvent:Connect(function(plr, selBlock)
	selBlock2 = selBlock
	selBlock2.Anchored = true
	selBlock.CanCollide = true
	orient = Vector3.new(0, 0, 0)
	selBlock2.Orientation = orient
end)

RS.ReplicateHit.OnServerEvent:Connect(function(plr, ASHV)
	script.MouseHit.Value = ASHV
	selBlock2.CFrame = script.MouseHit.Value
	selBlock2.Orientation = orient
end)

RS.RotationHit.OnServerEvent:Connect(function(plr)
	orient = orient + Vector3.new(0, 45, 0)
	print(selBlock2.Orientation)
end)

Also for anyone who wants to see the bug in person here is the game Hold It Simulator: A New Shine - Roblox

2 Likes

You can detect if a part is clipping with another part using:

part:GetTouchingParts()

You could then check if a part being held is clipping every frame, then when the player releases the part it moves the part to the last position where it wasn’t clipping.

(A fancier solution might be to check positions between the last “safe” position and the subsequent “clipping” position for the first in-between placement that’s not clipping.)

I don’t think it’s using physics, so probably more like WorldRoot:GetPartsInPart()

2 Likes

I believe GetTouchingParts should still work (the exception where it doesn’t work is for CanCollide off I thought), but yes the newer function would be better to use instead.

Will try that. I’ll reply if it doesn’t work

Thank you! It worked like a miracle!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.