Door Not Closing

Earlier I wrote a topic about how my door script wasn’t working, I was helped and it worked. I wanted to make it when the leave, it closes the door.
I get no errors:

local RP = script.Parent.PrimaryPart
local Radius = 10
local DetectionPart = script.Parent.Detectionpart
local TweenService = game:GetService("TweenService")
local DoorTween = TweenService:Create(RP, TweenInfo.new(0.25,Enum.EasingStyle.Sine), {CFrame = RP.CFrame * CFrame.Angles(0,math.rad(-180),0)})
local EndDoorTween = TweenService:Create(RP, TweenInfo.new(0.5,Enum.EasingStyle.Sine), {CFrame = RP.CFrame * CFrame.Angles(0,math.rad(180),0)})
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	game:GetService("RunService").Heartbeat:Connect(function()
		local Character = plr.Character or plr.CharacterAdded:Wait()
		local door = {}
		local HRP = Character.HumanoidRootPart
		if (HRP.Position - DetectionPart.Position).Magnitude <= Radius then
			DoorTween:Play()
			wait(0.2)
			door.Open = true
		elseif (HRP.Position - DetectionPart.Position).Magnitude <= Radius and HRP.Position - DetectionPart.Position.Magnitude <= Radius then
			EndDoorTween:Play()
			
		end
	end)
local debounce = false
	DoorTween.Completed:Connect(function(comp)
		if not debounce then
			debounce = true
			print("Door, One, Near Baseplate: Has Opened")
			wait(5)
			debounce = false
		end
		local d = false
		EndDoorTween.Completed:connect(function(comp)
			if not d then
				d = true
				print("Door, Two, Near Baseplate: Has Opened")
				wait(5)
				d = false
			end
		end)
		
	end)
end)

This is a video on what happens when I run it:

I couldn’t upload the video, if anyone wants i can upload it to youtube and send it here.

Why are you encasing both of the DoorTween.Completed functions inside of each other? Shouldn’t you make them both separate apart?

    local debounce = false
	DoorTween.Completed:Connect(function(comp)
		if not debounce then
			debounce = true
			print("Door, One, Near Baseplate: Has Opened")
			wait(5)
			debounce = false
		end
    end)

	local d = false
	EndDoorTween.Completed:connect(function(comp)
		if not d then
			d = true
			print("Door, Two, Near Baseplate: Has Opened")
			wait(5)
			d = false
		end
	end)

end)
1 Like

Sorry for late response, I fixed it but the same thing is happening

Ok what are you doing in this line then? :thinking: You just basically copied & pasted the same script from the first if statement, which is basically overlapping each other’s tweens

Try this?

Players.PlayerAdded:Connect(function(plr)
	game:GetService("RunService").Heartbeat:Connect(function()
		local Character = plr.Character or plr.CharacterAdded:Wait()
		local door = {}
		local HRP = Character.HumanoidRootPart
		if (HRP.Position - DetectionPart.Position).Magnitude <= Radius then
			DoorTween:Play()
			wait(0.2)
			door.Open = true
		elseif (HRP.Position - DetectionPart.Position).Magnitude > Radius then
			EndDoorTween:Play()
		end
	end)
1 Like

Well it doesn’t exactly work, but its getting somewhere, now the door immediatly opens when i test the game and its printing both of the things i put in the .Completed functions:


even though its doing that is staying in that position no mater what

I’ll continue doing debugging and stuff for now, if you spot anything else please tell me.

I think we’re almost there, & I think we can fix it, since both of your debounces are treated differently we can implement a couple more checks:

LET’S TRY THIS!

local RP = script.Parent.PrimaryPart
local Radius = 10
local DetectionPart = script.Parent.Detectionpart
local TweenService = game:GetService("TweenService")
local DoorTween = TweenService:Create(RP, TweenInfo.new(0.25,Enum.EasingStyle.Sine), {CFrame = RP.CFrame * CFrame.Angles(0,math.rad(-180),0)})
local EndDoorTween = TweenService:Create(RP, TweenInfo.new(0.5,Enum.EasingStyle.Sine), {CFrame = RP.CFrame * CFrame.Angles(0,math.rad(180),0)})
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	game:GetService("RunService").Heartbeat:Connect(function()
		local Character = plr.Character or plr.CharacterAdded:Wait()
		local door = {}
		local HRP = Character.HumanoidRootPart
		if (HRP.Position - DetectionPart.Position).Magnitude <= Radius then
			DoorTween:Play()
			wait(0.2)
			door.Open = true
		elseif (HRP.Position - DetectionPart.Position).Magnitude > Radius then
			EndDoorTween:Play()
		end
	end)

    local DB1 = false
	local DB2 = false

	DoorTween.Completed:Connect(function(comp)
		if not DB1 then
			DB1 = true
			print("Door, One, Near Baseplate: Has Opened")
			wait(5)
			DB1 = false
		end
    end)

	EndDoorTween.Completed:connect(function(comp)
		if not DB2 then
			DB2 = true
			print("Door, Two, Near Baseplate: Has Opened")
			wait(5)
			DB2 = false
		end
	end)

end)

Darn, Nothing is happening now, no errors, nothing:


I think I’m gonna print (HRP.Position - detectionPart.Position).Magnitude and see what I get.

I’m gonna try to put it in server script service and see if that changes anything.

I missed something, could you try it again?

Same thing happens, I put a wait before the function because I noticed that the door already opens when I run the game, And I wanted to see it do that, but when I put a wait(5) nothing happens over 5 seconds, really weird to be honest:

Ok yeah I did a mistake, just copy/paste the code again I fixed it

Weird situation though

Ok, so now I know that the checking thing works, but the door is still having trouble. I remember I was making a tween on how the block would follow the players head, and I had trouble with it. And this guy told me it was because I had the tween defined outside the loop. I don’t know if trying that would do anything, I’ll update you on how it goes

Wow, now its just going in a 360 circle,I got a error in the checking thing because the tween is defined in the function now. I’ll keep on working on it.

Alright! It works now, I just need to debounce it because it goes crazy when you enter the 10 stud radius.

So how to fix it was I was suppose to define the tween in the “if” statement:

if (HRP.Position - DetectionPart.Position).Magnitude <= Radius then
			local DoorTween = TweenService:Create(RP, TweenInfo.new(0.25,Enum.EasingStyle.Sine), {CFrame = RP.CFrame * CFrame.Angles(0,math.rad(-180),0)})--its defined in the "if" statement so it will work.
			DoorTween:Play()
			wait(0.2)
			door.Open = true

bad example:

local DoorTween = TweenService:Create(RP, TweenInfo.new(0.25,Enum.EasingStyle.Sine), {CFrame = RP.CFrame * CFrame.Angles(0,math.rad(-180),0)}) --I kept it out of the "if" statement, therefore it simply won't work.
if (HRP.Position - DetectionPart.Position).Magnitude <= Radius then
			DoorTween:Play()
			wait(0.2)
			door.Open = true