Touched (Event): Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mr Doom Bringer
No edit summary
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">"
 
(22 intermediate revisions by 9 users not shown)
Line 1: Line 1:
<onlyinclude>
<onlyinclude>{{Event
{{Event|
|name = Touched
name = Touched
|arguments = [[RBX.lua.BasePart (Object)|BasePart]] <var>otherPart</var>
|arguments = [[Instance]] ''part''
|description = Fired when another object comes in contact with this object.
|description = Fired when another part comes in contact with this object
|object = BasePart
|object = [[RBX.lua.FlagStand (Object)|Flagstand]], [[RBX.lua.Part (Object)|Part]], [[RBX.lua.Seat (Object)|Seat]], [[RBX.lua.SpawnLocation (Object)|SpawnLocation]]
}}</onlyinclude>
|}}
 
</onlyinclude>
{{clear floats}}
 
{{Example|This code would print the name of any object touching the part:
<syntaxhighlight lang="lua">
Workspace.Part.Touched:connect(function(otherPart)
  print(otherPart)
end)
</syntaxhighlight>}}
 
{{EmphasisBox|'''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 <var>otherPart</var> argument's [[Parent (Property)|Parent]] to be nil. Scripts that use the [[Touched (Event)|Touched]] event should check for this, otherwise they will break if they try to use members of <var>otherPart</var>'s parent.|red|dark=yes}}
 
[[Category:Events]]

Latest revision as of 06:14, 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:
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.