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

How to tell if a part is not being touched?

Asked by 8 years ago

I have a door that slides open when you touch a certain part, and closes automatically after 5 seconds. Problem is, I want it to close only if that part is not being touched. Is there a way to say something like:

1if Part.Touched ~= true then

or something similar? (Assume Part is the part that you touch to open the door.) This is my current script:

01door = script.Parent.Parent.DoorSlide
02isOpen = script.Parent.Parent.IsOpen.Value
03local Debounce = false
04 
05function MoveOpen()
06    if Debounce then return end
07    if isOpen == false then
08    Debounce = true
09    for i = 1, 80 do
10        door.CFrame = door.CFrame + Vector3.new(-0.05, 0, 0)
11        wait()
12    end
13    isOpen = true
14    Debounce = false
15    end
View all 29 lines...

Any help would be appreciated, even if the answer is quite obvious.

1 answer

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

Just check if the number of Touching parts is 0.

There's a neat function of parts called GetTouchingParts and it returns a table of all parts currently in contact with the part. Just check if the number of parts is zero like so,

1local part = script.Parent
2 
3if #part:GetTouchingParts() > 0 then
4    print("I'm being Touched!")
5else
6    print("I'm lonely.")
7end
This does not work on models, but should work on anything else. Can be used in any type of script.

Good Luck!

If I helped you, please don't forget to accept my answer.
Ad

Answer this question