Right now I have a script that detects if a player's position is within a certain distance of a door using magnitude, if the player is in the position, the door will open and then close after a wait of 2 seconds. However, I want to make it so that if the player is still within the certain distance for the door to stay open and close ONLY after the player leaves that distance. How can I do this?
My code so far
debounce = true
local door = game.Workspace.Door
local sound = game.Workspace.Door.Sound
function onTouched(hit)
if debounce == true then
debounce = false
if (hit.Parent:findFirstChild("Humanoid") ~= nil) then
if (hit.Parent.Torso.Position - self.Position).magnitude <=10 then
if inProgress == false then --if door is closed
inProgress = true
sound:Play()
door.Transparency = 1
wait(2)
sound:Play()
door.Transparency = 0
inProgress = false
end
end
end
wait(1)
debounce = true
end
end
script.Parent.Touched:connect(onTouched)

2 comments
Thank you! You sir are a genius! I did not even know you could use repeat and until like that, I thought it was only for while/ for loops, it works exactly as I wanted it to, I changed > 10 to >= 10. Wow...thanks I can't believe it! Was stuck on this for hours!
Your Welcome!