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

How to Know When a Player is In Contact with a Part/Union?

Asked by 5 years ago

I just need to know the basic format for a function that activates when a person comes into contact with a part, as well as a corresponding function that runs when the contact is lost. Thanks so much in advance.

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

To fire when someone makes contact with a part, we would use the inbuilt "Touched" event. You can connect that with the lines below:

script.Parent.Touched:Connect(function()
    -- I always recommend testing if it was a player, I do that like so:
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum then
        -- do something
    end
end)

The script would have to be parented to the part that you want to connect the touched event to for those lines to work. However, you CAN connect it from a different area such as ServerScriptService.

To fire when someone stops touching a part, you can use the inbuilt TouchEnded event. You can connect that one with the same code block, only changing "Touched" to "TouchEnded". Feel free to comment if you have any questions.

Ad

Answer this question