I'm new to scripting and I'm doing a simple game. When I click the block that this script is in it works the first time, it stays opens then closes automatically but when I click it again it just stays open. How can I make this close automatically on the other tries?
local block = game.Workspace.ObbyI.blockI function onClicked() block.Transparency = .7 block.CanCollide = false end script.Parent.ClickDetector.MouseClick:connect (onClicked) wait(10) block.Transparency = 0 block.CanCollide = true
I think it is due to your wait()
, wait() yields the script meaning it will not run the codes after for the next 10 seconds. You might also want to add a Debounce. This works, but it isn't completely reliable. It can be spammed, therefore we need to add a Debounce that checks if the door is open or not.
I would move the wait()
inside the onClicked() function.
local block = game.Workspace:WaitForChild("ObbyI"):WaitForChild("blockI") local IsOpen = false function OnClicked() if not IsOpen then -- Checking if the door is open or not. If the door isn't open, then we will open it else we want it to remain closed. IsOpen = true block.Transparency = 0.7 block.CanCollide = false end wait(10) block.Transparency = 0 block.CanCollide = true IsOpen = false end script.Parent.ClickDetector.MouseClick:Connect(onClicked)