Trying to make the player able to open and close the door when clicked. How can I achieve that?

Working on a garage door and I’m trying to make the player able to open and close the door when clicked. How can I achieve that?

Code:

local garageDoor = script.Parent
local clickDetector = script.Parent.ClickDetector
local DEBOUNCE = false
local amount = 1

clickDetector.MouseClick:Connect(function()
	if not DEBOUNCE then
		DEBOUNCE = true
        --Door open
		garageDoor.Size = Vector3.new(1, 2, 16)
		garageDoor.Position = Vector3.new(14.5, 10, -12)
		task.wait(amount)
		DEBOUNCE = false
	end
end)

--Door close
--garageDoor.Size = Vector3.new(1, 10, 16)
--garageDoor.Position = Vector3.new(14.5, 6, -12)

You could make a new variable that indicates wether the door is open or not and then do the changes needed to close or open the door.

It would look something like this:

local garageDoor = script.Parent
local clickDetector = script.Parent.ClickDetector
local DEBOUNCE = false
local OPEN = false
local amount = 1

clickDetector.MouseClick:Connect(function()
	if not DEBOUNCE then
		DEBOUNCE = true

		if not OPEN then
            --Door open

            OPEN = true

            garageDoor.Size = Vector3.new(1, 2, 16)
		    garageDoor.Position = Vector3.new(14.5, 10, -12)
        else
            --Door close

            OPEN = false

            garageDoor.Size = Vector3.new(1, 10, 16)
            garageDoor.Position = Vector3.new(14.5, 6, -12)
        end

		task.wait(amount)

		DEBOUNCE = false
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.