This tutorial on how to make a very basic opening and closing door will require basic scripting knowledge.
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
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
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.