How To Make A Basic Door

This tutorial is about making a basic opening and closing door.

by MomentumKnight

Author Avatar

This tutorial on how to make a very basic opening and closing door will require basic scripting knowledge.

You will need:

  1. A part (This will be the door)
  2. Clickdetector (Place this inside the part)
  3. Script (Place this inside the part)

Variables

Creating variables is essential to creating clear code.

local Door = script.Parent -- This is the door
local ClickDetector = Door.ClickDetector -- This detects player clicks
local DoorIsOpen = false -- This checks if the door is open or closed

Functions

These functions will be called when the door is clicked or when we want to open or close the door.

local Door = script.Parent -- This is the door
local ClickDetector = Door.ClickDetector -- This detects player clicks
local DoorIsOpen = false -- This checks if the door is open or closed

local function openDoor() -- This function is run when we want to open the door
    DoorIsOpen = true
    Door.CanCollide = false
    Door.Transparency = 0.5
end

local function closeDoor() -- This function is run when we want to close the door
    DoorIsOpen = false
    Door.CanCollide = true
    Door.Transparency = 0
end

local function doorClicked() --This function is run when the door is clicked
    if DoorIsOpen then -- If the door is open then close the door
        closeDoor()
    else --If the door is closed then open the door
        openDoor()
    end
end

Putting It All Together

local Door = script.Parent -- This is the door
local ClickDetector = Door.ClickDetector -- This detects player clicks
local DoorIsOpen = false -- This checks if the door is open or closed

local function openDoor() -- This function is run when we want to open the door
    DoorIsOpen = true
    Door.CanCollide = false
    Door.Transparency = 0.5
end

local function closeDoor() -- This function is run when we want to close the door
    DoorIsOpen = false
    Door.CanCollide = true
    Door.Transparency = 0
end

local function doorClicked() --This function is run when the door is clicked
    if DoorIsOpen then -- If the door is open then close the door
        closeDoor()
    else --If the door is closed then open the door
        openDoor()
    end
end

ClickDetector.MouseClick:Connect(doorClicked)
-- We will connect the doorClicked function to the ClickDetector MouseClick event so when the player clicks the door, the function will trigger.

View in-game to comment, award, and more!