I tried it it did not work the hit prints but only when the player touches it
You can check for the ClassName
of the object that touched the part. For example, a BasePart
would encapsulate multiple subclasses, such as normal Parts
, WedgePart
, etc. – If you wanted to be more specific, make sure you don’t check for BaseParts
.
This utilizes the :IsA()
method of an Instance, which you can find more information about on this Developer Hub page.
Example Code Block
local part = script.Parent
part.Touched:Connect(function(hit)
if hit:IsA("BasePart") then -- This checks what kind of Instance touched it
-- If it was a BasePart, then the part that contains this script would be destroyed
part:Destroy()
end
end)
Keep in mind that over a large scale, this would not be very efficient to have in every single part you need it for. Usage of the CollectionService
would allow you to keep it centralized so that modifications can be easily made if necessary, and it also makes it more organized in the long term.
parent it to the part you want to detect
I tried it but the part didn’t destroy the other part but instead the player destroys it
Could you clarify which part is supposed to be destroyed? In the original post you provided, you destroyed script.Parent
, which is the same part you’re listening for the Touched
event on. I followed this same idea in the example codeblock since it appeared that you wanted the part with the script inside of it to be destroyed upon contact.
Under the assumption that a player’s Character touches one of these parts, do you want the part from the Character to be destroyed, instead? If so, you can instead call :Destroy()
on the object that touched the part:
local part = script.Parent
part.Touched:Connect(function(hit) -- "hit" will refer to the Instance that touched the part
if hit:IsA("BasePart") then
hit:Destroy() -- Destroys the Instance that touched the part
end
end)
ok I’m gonna make this understandable
Part A is what the player controlling right now. When the player click a button it will do it’s thing.
What I wanted the achieve is when ever Part A touches another part it will destroy that part
this has nothing to do with the player.
I’m sorry if I made you confused also for the late response
I utilized a player’s Character as an example. Based on the explanation you provided here, the most recent codeblock will still apply for that use case.
I tried it but it’s still doesn’t work.
Is anything showing up in the output? Try adding print statements within the function to see when it’s being activated & if any of the conditions are being met.
Example:
local part = script.Parent
print(part.Name.." exists!")
part.Touched:Connect(function(hit)
print(hit.Name.." has touched the part")
if hit:IsA("BasePart") then
warn(hit.Name.." is a BasePart. It is being destroyed")
hit:Destroy()
end
end)
It did not work for the parts but work for the player
the print :
print(part.Name.." exists!")
I also turn on the CanCollide and turn off the Anchored in each 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).
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()