Keep door open if player is still in trigger box

Hi,

As the title says, how do I make the door remain open if the player is still in triggerbox?

image

local model = script.Parent

local trigger_in = model.Trigger_In

local trigger_out = model.Trigger_Out


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

	true, --Reverse

	0 --DelayTime



)


local function open(otherPart)

	local player = game.Players:FindFirstChild(otherPart.Parent.Name)

	if player and not activated 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()



		activated = false

	end

end


trigger_in.Touched:Connect(open)

trigger_out.Touched:Connect(open)

local trigger_in = model.Trigger_In

local trigger_out = model.Trigger_Out
1 Like

I would probably try use Touched and TouchEnded events. I would also add a debounce to prevent any spam of the part.

1 Like

didnā€™t test it but maybe this could work better. using only one part to trigger your tween and creating another tween info for the closing tween instead of trying to reverse it


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()
		
		trigger_in.TouchEnded:Connect(function()
			activated = false
			-- close doors
			
		end)
		

	end
end


trigger_in.Touched:Connect(open)

1 Like

Hi,

It works, but it remains open and doesnā€™t close again.

1 Like

Why putting TouchEnded function INSIDE the open function?! Try this code instead:

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 am very sorry if I criticizes you too harsh. The script you gave were kind of, redundant?)

The tween closing animation is just the original position

Bumping if anyone wants to help, all help is appreciated.

1 Like

They worked, the one in my post. It opened and closed the door, but I want it to stay open if the player is still in the tirgger box.

According to my knowledge, Touched had already done that for you. It kinda just stays open when youā€™re still in that part.

But how do I make it close if the player is out of the hitbox?

TouchEnded had done that for you, again. When you get out of the part, it closes.

i would use Zone+ then do something like

local ZoneM = require(path)
local NewZ = ZoneM.new(area) -- eg the hitbox surrounding the door

NewZ.playerEntered:Connect(function(player)
       --Open door
end)

NewZ.playerExited:Connect(function(player)
       --Close door
end)



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