Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make door stay open if player is within magnitude?

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)

1 answer

✓ Accepted answer

You could make a hinge constraint on the door and set it's Actuator type to Servo. Then when the player comes close enough, you can set the target angle to 90 degrees (or whatever angle you want).

Then when the player leaves, set the target angle to 0 degrees.

The cool thing about this is that also you can change how fast you want the door to open and close by changing the angular speed.

But, that would change alot of stuff

MY REAL SOLUTION

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
                    -- Here's what i changed
                    repeat
                    wait(1)
                    until (hit.Parent.Torso.Position - self.Position).magnitude >10 

                    sound:Play()
                    door.Transparency = 0   
                    inProgress = false

                end
            end
        end
        wait(1)
        debounce = true
    end
end

script.Parent.Touched:connect(onTouched)

2 comments

0

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!

0

Your Welcome!

This discussion is preserved as it was. New answers, comments, and votes are closed.