Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you do something when someone touches something?

Asked by 9 years ago

As the question says, I know how to do the onChatted, but how do I make something happen when a player runs into a part?

1 answer

Log in to vote
2
Answered by
Merely 2122 Moderation Voter Community Moderator
9 years ago

Use the .Touched event.

The Touched event has one argument - the other part that touched it. To tell if the other part belongs to a player, you have to check if that part is inside a Model with a Humanoid object.

game.Workspace.Part.Touched:connect(function(otherPart)
   --check if the other part belongs to a character
   if (otherPart.Parent and otherPart.Parent:FindFirstChild("Humanoid")) then
      local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
      if player then
         print("Touched by " .. player.Name)
      end
   end
end)
Ad

Answer this question