How could I make this code work with bigger parts?

This code works fine when ‘part’ touches small parts, but it doesn’t work with big parts.

local Part = workspace.Part

Part.Touched:Connect(function(Hit : BasePart)
	local Unit = (Part.Position - Hit.Position).Unit
	Part.Position += Unit
end)

Does anyone know how to fix this?

2 Likes

1st of all, make sure the big parts that they are touching does have CanTouch enabled. 2nd, try raycasting to detect touches.

The big parts already have cantouch on, also wouldn’t raycasting be laggy?

Raycast it on client, send info to the server if a result occurs.

First raycasts are way more efficient than .Touched and way more precise, they does not cost at all a lot performance. Second as I can see you are trying to prevent part collides? Then you opt to use raycasts but not difference of position, else it might be extremely inaccurate dealing with huge or extreme vector objects.

Here is what I’m trying to make it do:
When partA touches partB partA gets moved back.


It works fine when partB is small, but if it’s big the position is too far from partA and it messes everything up.

When partB is small partA gets pushed back just as it should.


But when partB is big it doesn’t work well, it pushes partA off to the side.

I guess you can use this method: https://devforum.roblox.com/t/detect-what-face-was-touched/1940320/2?u=soprosostupid to move the part on a axis according to the face touched

Are these Anchored Parts?

You could also calculate the distance to the edge of both Parts (for example if Part A is 10x 4 x 12 studs you could take its Position and calculate the edge faces to be 1/2 the X,Y,Z values.
Do that for both Parts and you can use math to determine if the edges are at the same point and move them according to their Positions + or - the 1/2 X,Y,Z values.

I usually use GetTouchingParts instead of Touched, since touched is a lot (lot is an understatement) less accurate.

If the parts are cancollide off. You’ll have to do a little “hack” to get it working.

local TouchThread = part.Touched:Connect(function()end) --// Start the touch event.
local parts = part:GetTouchingParts() --// Returns Table full of parts. Look for the part(s) that push it.
TouchThread:Disconnect() --// Kill the Touched Event.

It’s cause sometimes touch isn’t being activated.

I will see if I can get that to work.