Touched (Event): Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
No edit summary
m Text replacement - "<code lua>" to "<SyntaxHighlight code="lua">"
Line 9: Line 9:


{{Example|This code would print the name of any object touching the part:
{{Example|This code would print the name of any object touching the part:
<code lua>
<SyntaxHighlight code="lua">
Workspace.Part.Touched:connect(function(otherPart)
Workspace.Part.Touched:connect(function(otherPart)
   print(otherPart)
   print(otherPart)

Revision as of 03:42, 27 April 2023

Touched ( BasePart otherPart )
Description Fired when another object comes in contact with this object.
Member of: BasePart


Example
This code would print the name of any object touching the part:

<SyntaxHighlight code="lua"> Workspace.Part.Touched:connect(function(otherPart)

 print(otherPart)

end)


Note: Lots of objects (such as projectiles in weapons) have scripts that remove the part as soon as it hits something. This means that it is possible for the otherPart argument's Parent to be nil. Scripts that use the Touched event should check for this, otherwise they will break if they try to use members of otherPart's parent.