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

Why does it only automatically close the first time?

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

1 answer

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)

7 comments

0

It didn't work When I tried this script it said attempt to call a nil value

0

Give it a try now, I edited it.

0

It says the same thing

0

Can you tell me the exact error?

0

It says "attempt to call a nil value" (sorry for the late reply)

0

Which line? If you look at the output, it tells you which line it is on.

0

It literally only says "18:41:43.171 - attempt to call a nil value" Also when I click it the door doesn't open

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