I need someone who can review my first script, a button clicker

Good day! Today I wrote my first and own script for the first time. It is a door script in which when you press the button that the door opens. So it’s actually very simple. The script works, but I’m not sure if I made this as easy as possible or if there is an easier and more efficient way to do it. :slight_smile:

Here is the code of the working door:

(upload://xshWqQbF2y2aMxqsNPTttmGwgkO.jpeg)

local button = script.Parent.Parent
local wood = script.Parent.Parent.Parent.Wood

function onClick()
	button.CanCollide = false
	wood.CanCollide = false
	wood.Transparency = 0.3
	wait(3)
	button.CanCollide = true
	wood.CanCollide = true
	wood.Transparency = 0
end

clickDetector.MouseClick:Connect(onClick)```

It’s a good script, but I would add a cooldown for it, this is also known as a debounce.

local button = script.Parent.Parent
local wood = script.Parent.Parent.Parent.Wood
local canClick = true

function onClick()
	if canClick then --Checks if canClick is true
		canClick = false --Then sets it to false, so this function can't run again
		button.CanCollide = false
		wood.CanCollide = false
		wood.Transparency = 0.3
		wait(3)
		button.CanCollide = true
		wood.CanCollide = true
		wood.Transparency = 0
		canClick = true --Sets it back to true for re-use
	end
end

clickDetector.MouseClick:Connect(onClick)
4 Likes

Alright, thank you for your help! :slight_smile:

1 Like

script looks great, however, i suggest tweening the door as it gives it more of a modern feel. (where the door actually opens up)

1 Like