Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to tell if a part is not being touched?

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

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.