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

Can Someone Help Me With This ClickDetector Script For A Door?

Asked by 6 years ago

Yeah So For Another One Of My Games,I Am Gonna Put A Door That Players Can Click So When They Click,It Opens.But Since I Have Got 2 Seperate Bits Of The ClickDetector The Door Doesn't Open?

Here It Is:

local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function(player)
    workspace["Door"].Transparency = 1
    workspace["Door"].CanCollide = false
end)


local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:Connect(function(player) 
    workspace["Door"].Transparency = 0
    workspace["Door"].CanCollide = true
end)

1 answer

Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years ago

To fix this you're going to want to combine the two using an if statement and a boolean to check after the player has clicked the button. If they did then make the boolean true or false depending if its open or closed.

local clickdetector = script.Parent:WaitForChild("ClickDetector")

local doorOpened = false

clickdetector.MouseClick:Connect(function(player)
    if doorOpened == false then --If our door is closed then continue our next code
        doorOpened = true --Make our boolean true since its closed

        workspace["Door"].Transparency = 1
        workspace["Door"].CanCollide = false
    else --If door is already open then instead do:
        doorOpened = false

        workspace["Door"].Transparency = 0
        workspace["Door"].CanCollide = true
    end
end)
Ad

Answer this question