Keep door open if player is still in trigger box

Also… make your hit box taller, so that it will detect the player’s HumanoidRootPart

1 Like

The script you sent me didn’t do that. It didn’t close again.

Already did, but changed nothing :confused:

Look to the code I posted on that link. The person had the same issue, detecting when entering and leaving a hit box

I think you need to determine if the issue is your detecting hitboxes correctly or if its a problem with the mechanic of the door opening and closing.

maybe use some print statements that print “Open” and “Close” to make sure the hitbox stuff works first. Then if it does, but doors not opening or closing properly, then work on the mechanic of the doors

Are you sure you’re going in the hitbox and then exiting the hitbox back and fourth (at a fast pace)? If so, must’ve been 2 tweens (in one part) are playing at the same time, which you know, break the tweens.

The script I’m using is the one you sent me. This is what happens in video:

And this is the script you gave me:

local model = script.Parent

local trigger_in = model.Trigger_In


local left = model.MainL

local right = model.MainR


local activated = false


local TweenService = game:GetService("TweenService")


local tweenInfo = TweenInfo.new(



	model.Speed.Value, --Time

	Enum.EasingStyle.Bounce, --Easing Style

	Enum.EasingDirection.Out, --EasingDirection

	0, --Repeat Count

	false, --Reverse

	0 --DelayTime



)


local function open(hitPart)

	local player = game.Players:GetPlayerFromCharacter(hitPart.Parent)

	if player and activated == false then

		activated = true


		local tweenL = TweenService:Create(left, tweenInfo, {Position = model.TargetL.Position})

		local tweenR = TweenService:Create(right, tweenInfo, {Position = model.TargetR.Position})



		tweenL:Play()

		tweenR:Play()

		tweenR.Completed:Wait()
	end
end

local function close(hitPart)
	local player = game.Players:GetPlayerFromCharacter(hitPart.Parent)
	if player and activated == true then
		activated = false
		--Create the Tween closing animation by yourself.
	end
end

trigger_in.Touched:Connect(open)
trigger_in.TouchEnded:Connect(close)

I forgot there’s a Completed event in the function. Remove that.
Weird, if that’s the case, then I’ll try to do something.

As I said, put some print statements or breakpoints to make sure its getting into that ‘close’ funcion

1 Like

…Are you sure that you made both closing tweens? Because I just took a glance and there isn’t any?

I get this

I put print statements in the activated and I get this.

There isn’t a tween for closing, I’m not sure how to do that. The tween for opening goes to target L and Target R. How do I tween it to go back to its original position?

Well, make 2 parts and put both of them inside the doors. It should be getting you the original position. Then you can tween it using those 2 parts. Oh, make sure it is transparent and non-collidable. I mean, we wouldn’t like parts being visible and blocking the door entrance, right?

(PS: I thought CFrame was gonna work, but I guess not, since Position kind of overrides it.)

Here is from my own door’s code

local tweenInfo = TweenInfo.new(
	1,
	Enum.EasingStyle.Quart,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local tweenOpenRight = game.TweenService:Create(motorRight,tweenInfo,{TargetAngle=100})
local tweenOpenLeft = game.TweenService:Create(motorLeft,tweenInfo,{TargetAngle=100})
local tweenCloseRight = game.TweenService:Create(motorRight,tweenInfo,{TargetAngle=0})
local tweenCloseLeft = game.TweenService:Create(motorLeft,tweenInfo,{TargetAngle=0})

		tweenOpenRight:Play()
		tweenOpenLeft:Play()
		tweenOpenRight.Completed:Wait()


		tweenCloseRight:Play()
		tweenCloseLeft:Play()
		tweenCloseRight.Completed:Wait()

Can I use CFrame instead. I don’t like this tween

CFrames are perfectly okay. They’re the same thing with Position.

With a bit of help from @SelDraken’s solution to “Touched is endlessly turned on and off”, I finally got it working!

local model = script.Parent

local trigger_in = model.Trigger_In


local left = model.MainL

local right = model.MainR


local activated = false


local TweenService = game:GetService("TweenService")


local tweenInfo = TweenInfo.new(



     model.Speed.Value, --Time

     Enum.EasingStyle.Bounce, --Easing Style

     Enum.EasingDirection.Out, --EasingDirection

     0, --Repeat Count

     false, --Reverse

     0 --DelayTime



)

local CFrame1 = CFrame.new(3,0,0)
local CFrame2 = CFrame.new(-3,0,0)

local CFrameRevert = {
     Original_CFrame_Left = left.CFrame;
     Original_CFrame_Right = right.CFrame
}

local tweenL = TweenService:Create(left, tweenInfo, {CFrame = left.CFrame * CFrame1})
local tweenR = TweenService:Create(right, tweenInfo, {CFrame = right.CFrame * CFrame2})

local tweenLClose = TweenService:Create(left, tweenInfo, {CFrame = CFrameRevert.Original_CFrame_Left})
local tweenRClose = TweenService:Create(right, tweenInfo, {CFrame = CFrameRevert.Original_CFrame_Right})

local function open(hitPart)

     local player = game.Players:GetPlayerFromCharacter(hitPart.Parent)

     if player and not activated and not player:GetAttribute("InBlock") and hitPart.Name == "HumanoidRootPart" then
          
          player:SetAttribute("InBlock",true)
          activated = true
          
          tweenL:Play()

          tweenR:Play()
     end
end

local function close(hitPart)
     local player = game.Players:GetPlayerFromCharacter(hitPart.Parent)
     if player and activated and hitPart.Name == "HumanoidRootPart" then
          activated = false
          player:SetAttribute("InBlock",false)
          tweenLClose:Play()
          tweenRClose:Play()
     end
end

trigger_in.Touched:Connect(open)
trigger_in.TouchEnded:Connect(close)

(PS: You do not need any “TargetL” or “TargetR” parts as this script will use CFrame.new instead.)
Also, the MainR and MainL must be a part. I think it’s a part already, but I’m just checking.

I went ahead and just made a sliding door. I had only done rotating doors before, so this was a nice learning experience.
AutomaticSlidingDoor.rbxl (41.4 KB)


local openDistance = 4 --how many studs the door will slide open
local speed = 1 --time for the door to open or close
local isOpen = false -- makes sure we wait until door is fully open before closing

local hitTable = {} --holds the id's of players that are standing in the hit box

local hitBox = script.Parent:WaitForChild("HitBox")
local closeSound = script.Parent:WaitForChild("SoundContainer"):WaitForChild("CloseSound")
local openSound = script.Parent:WaitForChild("SoundContainer"):WaitForChild("OpenDoor")

--====== Tweening Things ===========
local rightDoor = script.Parent:WaitForChild("RightDoor")
local leftDoor = script.Parent:WaitForChild("LeftDoor")

local closedLeft = leftDoor.PrimaryPart.CFrame
local closedRight = rightDoor.PrimaryPart.CFrame
local openLeft = leftDoor.PrimaryPart.CFrame + (leftDoor.PrimaryPart.CFrame.RightVector * -openDistance)
local openRight = rightDoor.PrimaryPart.CFrame + (rightDoor.PrimaryPart.CFrame.RightVector * openDistance)

local tweenInfo = TweenInfo.new(
	speed,
	Enum.EasingStyle.Quart,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local tweenOpenRight = game.TweenService:Create(rightDoor.PrimaryPart,tweenInfo,{CFrame = openRight})
local tweenOpenLeft = game.TweenService:Create(leftDoor.PrimaryPart,tweenInfo,{CFrame = openLeft})
local tweenCloseRight = game.TweenService:Create(rightDoor.PrimaryPart,tweenInfo,{CFrame = closedRight})
local tweenCloseLeft = game.TweenService:Create(leftDoor.PrimaryPart,tweenInfo,{CFrame = closedLeft})

--============= End Tweening Things =========================

function CheckForClose() --this checks to see if anyone is standing in the door and if not, closes the door
	if #hitTable < 1 then
		isOpen = false
		tweenCloseRight:Play()
		tweenCloseLeft:Play()
		closeSound:Play()
		tweenCloseRight.Completed:Wait()		
	end
end

hitBox.Touched:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if player and player.Character and player.Character.PrimaryPart == part then
		if not table.find(hitTable,player.UserId) then --see if player is not in list
			table.insert(hitTable,player.UserId) -- add player to list
		end
		if isOpen == false then
			closeSound:Stop()
			openSound:Play()
			tweenOpenRight:Play()
			tweenOpenLeft:Play()
			tweenOpenRight.Completed:Wait()
			openSound:Stop()
			isOpen = true
			CheckForClose() --this is called in case players left the hitbox while the dooor was still opening
		end
	end
end)

hitBox.TouchEnded:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if player and player.Character and player.Character.PrimaryPart == part then
		local index = table.find(hitTable,player.UserId) --find player in list
		if index then 
			table.remove(hitTable,index) --remove player from list
		end
		if isOpen == true then 
			CheckForClose() --see if the door needs to close, in case everyone is out of hit box, and door is fully open
		end
	end
end)

1 Like

Hi, Omg Thank youuu so much!!!

I feel like this can also be archived with worldroot:FindPartsInRegion3. I haven’t tried it before but given the name it should do what it’s intended