Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does it only automatically close the first time?

Asked by
natl1 2
4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)
0
It didn't work When I tried this script it said attempt to call a nil value natl1 2 — 4y
0
Give it a try now, I edited it. JoyfulPhoenix 139 — 4y
0
It says the same thing natl1 2 — 4y
0
Can you tell me the exact error? JoyfulPhoenix 139 — 4y
View all comments (3 more)
0
It says "attempt to call a nil value" (sorry for the late reply) natl1 2 — 4y
0
Which line? If you look at the output, it tells you which line it is on. JoyfulPhoenix 139 — 4y
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 natl1 2 — 4y
Ad

Answer this question