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

Can Someone Help Me With This ClickDetector Script For A Door?

Yeah So For Another One Of My Games,I Am Gonna Put A Door That Players Can Click So When They Click,It Opens.But Since I Have Got 2 Seperate Bits Of The ClickDetector The Door Doesn't Open?

Here It Is:

local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function(player)
	workspace["Door"].Transparency = 1
	workspace["Door"].CanCollide = false
end)


local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function(player) 
	workspace["Door"].Transparency = 0
	workspace["Door"].CanCollide = true
end)

1 answer

To fix this you're going to want to combine the two using an if statement and a boolean to check after the player has clicked the button. If they did then make the boolean true or false depending if its open or closed.

local clickdetector = script.Parent:WaitForChild("ClickDetector")

local doorOpened = false

clickdetector.MouseClick:Connect(function(player)
	if doorOpened == false then --If our door is closed then continue our next code
		doorOpened = true --Make our boolean true since its closed
	
		workspace["Door"].Transparency = 1
		workspace["Door"].CanCollide = false
	else --If door is already open then instead do:
		doorOpened = false

		workspace["Door"].Transparency = 0
		workspace["Door"].CanCollide = true
	end
end)

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