How do I detect if part touches another part

Could you clarify what you mean when you said that it “work for the player”? The script is located directly inside of a part, as mentioned in the original post, correct? If so, I can’t see anything else interfering with its functionality since you’ve confirmed that CanCollide is true for the parts that are intended to collide with other objects in the game.

The codeblock is essentially the same as the one you said that worked the opposite way around earlier on in this thread. The only difference is the Instance that is being destroyed.

what I mean about the “worked for the player”

when the player touches the part it’s body part is destroyed
but that isn’t what I wanted what I want is when Part A touches another part it will destroy it

the player has nothing to do with the destroying what it only does will control Part A

also about the cancollide thing I’m just saying it just incase it has something to do with it…

im gonna provide some vidoes just incase

In that case, you could add an additional check to ensure that no parts of the player’s Character are destroyed when coming into contact with the part that has the script.

I’m not very sure where the part is in relation to the player’s Character, but under the assumption that it’s parented directly to the Character, you could do the following:

local part = script.Parent
local Container = part.Parent

part.Touched:Connect(function(hit)

    local containerCheck = Container:FindFirstChild(hit)
--[[ This will check if it finds the Instance that touched the part inside of
the part's container (which could be the player's Character, in this case) --]]

    if hit:IsA("BasePart") and not containerCheck then

        hit:Destroy()
    end
end)

This will not account for parts within Accessories & anything else aside from the first layer of the Character, but until more information is provided about the hierarchy you have setup for this, this should allow for the issue to be resolved much more quickly.

the part has no relation towards the player

also here are the videos:

I don’t think that the Touched event fires when it intersects that other part in this case due to the manner in which its position is being altered. It’s being teleported to a new location immediately where a static part is and both of them remain static.

I just tested this in an empty baseplate where a different part would fall onto the one that has the script, and it got destroyed – a few seconds after runtime, the script then teleported the part with the script inside of the baseplate (by adjusting its position property), and the touched event was not fired.

In order to resolve this while maintaining usage of the Touched event, you’ll likely need to increment its position via loops/TweenService (from my understanding).

1 Like

Soooo I have to add wait when the part moves it’s position?

A wait won’t solve if it’s being brought to its new position instantaneously – I edited my post as you replied with a potential solution if you’d like to maintain usage of Touched events.

It’s past 4AM for me atm, so I may need to continue helping later on, if needed.

here are the one of the scripts from the Up,Down buttons

local part = game.Workspace.Part
local button = script.Parent

button.MouseButton1Click:Connect(function()
	local point = Vector3.new(1,0,0)
	part.Position = part.Position + Vector3.new(0,0,1)
end)

but i don’t know where to add the wait()

thanks for helping see you tmrw

1 Like

If your script don’t work, it is probably because you used CFrame or Position change. I writed a script below. You will have to put this script on yellow part.

script.Parent:GetPropertyChangedSignal("Position"):Connect(function()
    for count, instance in ipairs(script.Parent:GetTouchingParts()) do
        if instance.Name == "WhitePart" then
            instance:Destroy()
        end
    end
end)

The name of lthe ittle white part must be “WhitePart”

1 Like

I tried your script and i also parent it to the part the one who changes position
but sadly it did not work i checked the output there are no errors
i think the only solution is to make the moving to be smooth
but i dont know how to do it

Do you renamed the white part ?

yes I did rename the part to WhitePart

Try this, put this script in the part that you want to destroy the other parts. This will work even if the part has can collide off.

local part = script.Parent

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


while wait() do
   for I, v in pairs(GetTouchingParts(part)) do
     v:Destroy()
   end
end
script.Parent:GetPropertyChangedSignal("Position"):Connect(function()
    print(1)
    for count, instance in ipairs(script.Parent:GetTouchingParts()) do
        if instance.Name == "WhitePart" then
            print(2)
            instance:Destroy()
        end
    end
    print(3)
end)

What number is printes on this script when you move the yellow part at the same position than the white part?

I check the output there are no prints

Did you try my script, it should work.

where should i parent the script? just wanna know

The script should be a child of the main part. Like if the script was in PartA, whatever touched PartA will be destroyed.