Door usually glitches when opening or closing problem

Hello everyone, I have a problem that is related to the Touched and TouchEnded events and Lerping.

I am trying to create a door that you can open and close via a Touched event with the help of Lerping, however when you try to open the door it will usually glitch out but it will eventually open/close in the end so how do I fix this?

I tried adding a debounce but that didn’t fix the problem.

Here’s what would usually happen when you try to open it:

Also is Touched a good way to do this? or is there a better way?

I am currently using the Touched and TouchEnded event to do this, here’s how the script works:

Script
local door = game.Workspace.Door2
local checker = game.Workspace.Checker
local Inserter = game.Workspace.Inserter
touchedDelay = false

Inserter.Touched:Connect(function(hit)
if not touchedDelay then
	if hit.Parent.Name == "Level 1 Keycard" then
			for i = 0, 1, 0.01 do
				checker.Color = Color3.fromRGB(117,0,0)
				wait()
				door.Size = door.Size:Lerp(Vector3.new(0.05, 5.778, 0.311), i)
				door.CFrame = door.CFrame:Lerp(CFrame.new(92.447, 2.972, -23.83), i)
			end
		end
	end
	touchedDelay = true
end)

Inserter.TouchEnded:Connect(function()
		for i = 0, 1, 0.01 do
			checker.Color = Color3.fromRGB(0,117,0)
			wait()
			door.Size = door.Size:Lerp(Vector3.new(5.44, 5.668, 0.311), i)
			door.CFrame = door.CFrame:Lerp(CFrame.new(95.142, 2.917, -23.83), i)
		end
	wait(3)
	touchedDelay = false
end)

Because Touched event and TouchEnded event fire at the same time.

1 Like

If that’s the case then how do I fix it?

Well you could make a boolean value, set it to false. And then after a lerp is done you switch it to true.
Each value is a different lerp.

And you only need Touched event.

But I already did that, you mean I need to use a debounce right?

I did that, I tried using a debounce like I stated in the post but it didn’t work.

Inserter.Touched:Connect(function(hit)
	if not touchedDelay then
		touchedDelay = true
		if hit.Parent.Name == "Level 1 Keycard" then
			for i = 0, 1, 0.01 do
				checker.Color = Color3.fromRGB(117,0,0)
				wait()
				door.Size = door.Size:Lerp(Vector3.new(0.05, 5.778, 0.311), i)
				door.CFrame = door.CFrame:Lerp(CFrame.new(92.447, 2.972, -23.83), i)
			end
			wait(3)
			for i = 0, 1, 0.01 do
				checker.Color = Color3.fromRGB(0,117,0)
				wait()
				door.Size = door.Size:Lerp(Vector3.new(5.44, 5.668, 0.311), i)
				door.CFrame = door.CFrame:Lerp(CFrame.new(95.142, 2.917, -23.83), i)
			end
		end
		touchedDelay = false
	end
end)

I didn’t test it. But it should work.
Problem was that TouchEnded is fired after the Touch event itself

2 Likes

It still doesn’t work.

The same problem still happens, I don’t know what you did there but it didn’t work.

Try again, I forgot to remove end

1 Like

It is not debounce(to my opinion). It’s like a light switch. You touch it once, it is true(the light is lighted). You touch it again, it is false. So in this case. you touch it once, the open event happen, you touch it again, it closes.

1 Like

Is touched a good way to do this or is there a better way?

1 Like

.Touched() is good, most of games are using it.

3 Likes

Alright, thanks for helping the problem is now fixed.

1 Like