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

How to make a door open/close when clicked?

4 answers

local door=script.Parent
local cd=door:findFirstChild("ClickDetector")or Instance.new("ClickDetector",door)
cd.MouseClick:connect(function()
	door.CanCollide=not door.CanCollide
	door.Transparency=door.CanCollide and 0 or .5
end)

Turns the door half transparent and lets you walk through it if you click on it when it's closed, turns it solid when you close it. Inserts a ClickDetector if there isn't something named ClickDetector already there.

Add a ClickDetector object to the part. Then, in a script in the door, add the following code:

local door = script.Parent
local click = door.ClickDetector

click.MouseClick:connect(function(player)
	print(player.Name.." clicked the door!")
	-- Open/close the door, you should probably change this section depending on how the door works
	if door.CanCollide == true then
		door.CanCollide = false
		door.Transparency = 0.9
	else
		door.CanCollide = true
		door.Transparency = 0
	end
end)

Again, be sure to alter the code to fit your needs, all that is important is that you know how to open and close the door, and that you know that you use the ClickDetector object and watch for the MouseClick event.

1 comment

0

Thanks!

Find = script.Parent:FindFirstChild("ClickDetector")
if Find==nil then
Click_Detect = Instance.new("ClickDetector",script.Parent)
Click_Detect.MaxActivationDistance=8
else end
Door = script.Parent
Door.ClickDetector.MouseClick:connect(function(door)
Door.CanCollide = not Door.CanCollide
if Door.CanCollide==false then
	Door.Transparency=.9
elseif Door.CanCollide==true then
	Door.Transparency=0
end
end)

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