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:

if Part.Touched ~= true then

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

door = script.Parent.Parent.DoorSlide
isOpen = script.Parent.Parent.IsOpen.Value
local Debounce = false

function MoveOpen()
    if Debounce then return end
    if isOpen == false then
    Debounce = true
    for i = 1, 80 do
        door.CFrame = door.CFrame + Vector3.new(-0.05, 0, 0)
        wait()
    end
    isOpen = true
    Debounce = false
    end
--This is where I think I need to add something else, but I don't know what to add
wait(5)
if isOpen == true then
    Debounce = true
    for i = 1, 80 do
        door.CFrame = door.CFrame + Vector3.new(0.05, 0, 0)
        wait()
    end
    isOpen = false
    Debounce = false
end
end

script.Parent.Touched:connect(MoveOpen)

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,

local part = script.Parent

if #part:GetTouchingParts() > 0 then
    print("I'm being Touched!")
else
    print("I'm lonely.")
end
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