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.
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)