How would i made a door if a humanoid or a character is touching the sensor the door stays open

Hey! How would i made my door open and stay open when a humanoid or a char is touching it?
this is my script now

local door = script.Parent:WaitForChild("Door")
local sensor1 = script.Parent:WaitForChild("Sensor1")
local sensor2 = script.Parent:WaitForChild("Sensor2")
local noise = door:FindFirstChild("Noise")

local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1.79, Enum.EasingStyle.Linear, Enum.EasingDirection.In,0,false,0)

local doorPos1 = {CFrame = CFrame.new(-68.683, 10.023, -53.5)}
local doorPos2 = {CFrame = CFrame.new(-68.683, 3.25, -53.5)}

local open = tweenService:Create(door,tweenInfo,doorPos1)
local close = tweenService:Create(door,tweenInfo,doorPos2)

local db = false

sensor1.Touched:Connect(function()
	if db == false then
		db = true
	open:Play()
	wait(3.3)
		close:Play()
		wait(3.3)
		db = false
	end
end)

sensor2.Touched:Connect(function()
	if db == false then
		db = true
	open:Play()
	wait(3.3)
		close:Play()
		wait(3.3)
		db = false
	end
end)

hope someone can help! :grinning_face_with_smiling_eyes:

Using a Touched event for this may not be the best way.
How about checking if the Humanoid is between Sensor Position (sensor ± 4 studs, sensor +_ 4 studs, sensor +_ 4 studs), so basically within an 8 stud cube of the sensor’s Position and if they are keep the door open.

I would recommend against using the Touched event (for this instance)
Instead, you could do something else.
Choose a distance that the player must reach between them and the door in order for it to open/close.
Simply get the distance from the sensor and each character in the game, using a function similar to this:

--pass the sensor through this function
function FindClosestPlayer(part)
   if (not part) then return end
   local lowest = math.huge
   for _,v in ipairs(game:GetService("Players")) do
      local character = v.Character
      if (character) then
         local hrp = character.PrimaryPart
         if (hrp) then
            local distance = (hrp.Position - part.Position).magnitude
            if (distance < lowest) then
               lowest = distance
            end
         end
      end
   end
   return lowest
end

Check the distance every .1 to .2 seconds in a while loop in order to determine if you should open or close the door! Something like so

local closed = true --starts out closed

while wait(.1) do
   local lowest = FindClosestPlayer(sensor1)
   if (lowest <= 4) then
      if closed then
         --open door with tween
         closed = false
      end
   else
      if (not closed) then
          --close door with tween
          closed = true
      end
   end
end

Worked well for me in the past!

1 Like

You can use Region3 to detect if there is a player there. Do this

local function Check()
   local Door = nil -- Replace nil with a door instance

   local MinRegion = Door.Position - (Door.Size / 2)
   local MaxRegion = Door.Position + (Door.Size / 2)

   local DoorRegion = Region3.new(MinRegion, MaxRegion)

   local Whitelist = {} -- The whitelist of parts can be found by Region3, we will make it only detects 
player.

   for _, Player in pairs(game.Players:GetPlayers()) do
      if Player.Character then
         table.insert(Whitelist, Player.Character.PrimaryPart)
      end
   end

   local TouchingPart = workspace:FindPartsInRegion3WithWhiteList(DoorRegion, Whitelist)
   if #TouchingPart == 0 then
       print("No one touched the door, close it")
   else
       print("Someone touching, open the door")
   end
end

You should run it in a loop like check it every 1 seconds.

maybe you can use part.TouchEnded

.TouchEnded will some time tricky and may fail if there are more than 1 player. The best way is to get all the parts using Region3 but with Whitelist. After that check if there are more than 0 part inside the Region. If equal 0 mean no one touched, we will close the door. Otherwise, we will open it.

1 Like

Its touching the sensor not the door

Then change the door instance to the sensor.

I did change it it still didnt work

Is the sensor small or big?

30char30char

its about 6 studs wide almost 7

unkn232332own

You can make it bigger which cover the door, name it MainSensor and use it, make sure it’s invisible. Then keep the old Sensors but make it as a decoration.

unknown
This is the door and yea the sensor is originally invisible

Can you show me the code you’re currently using? Make sure you run it inside a loop

while wait(1) do
   Check()
end
local function Check()
	local Door = script.Parent:WaitForChild('Sensor1')
	
	local tweenService = game:GetService("TweenService")
	local tweenInfo = TweenInfo.new(1.79, Enum.EasingStyle.Linear, Enum.EasingDirection.In,0,false,0)

	local doorPos1 = {CFrame = CFrame.new(-68.683, 10.023, -53.5)}
	local doorPos2 = {CFrame = CFrame.new(-68.683, 3.25, -53.5)}
	
	local MinRegion = Door.Position - (Door.Size / 2)
	local MaxRegion = Door.Position + (Door.Size / 2)
	
	local open = tweenService:Create(Door,tweenInfo,doorPos1)
	local close = tweenService:Create(Door,tweenInfo,doorPos2)
	
	local DoorRegion = Region3.new(MinRegion, MaxRegion)

	local Whitelist = {Door}

	for _, Player in pairs(game.Players:GetPlayers()) do
		if Player.Character then
			table.insert(Whitelist, Player.Character.PrimaryPart)
		end
	end
	
	while wait(1) do
		Check()
	end
	
	local TouchingPart = workspace:FindPartsInRegion3WithWhiteList(DoorRegion, Whitelist)
	if #TouchingPart == 0 then
		close:Play()
	else
		open:Play()
	end
end



Thats what im using i dont usually use region3 so idk how it works :man_shrugging:

You are running the loop inside the function and not calling it, can you please use this instead?

local function Check()
   local Door = nil -- Replace nil with a door instance

   local MinRegion = Door.Position - (Door.Size / 2)
   local MaxRegion = Door.Position + (Door.Size / 2)

   local DoorRegion = Region3.new(MinRegion, MaxRegion)

   local Whitelist = {} -- The whitelist of parts can be found by Region3, we will make it only detects player.

   for _, Player in pairs(game.Players:GetPlayers()) do
      if Player.Character then
         table.insert(Whitelist, Player.Character.PrimaryPart)
      end
   end

   local TouchingPart = workspace:FindPartsInRegion3WithWhiteList(DoorRegion, Whitelist)
   if #TouchingPart == 0 then
       print("No one touched the door, close it")
   else
       print("Someone touching, open the door")
   end
end

while wait(1) do
   Check()
end
3 Likes

Just did that still didnt work :man_shrugging:

What type of Script you’re running? LocalScript or Script?

I am running it in a server script aka script.