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)
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
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.