I am making a script that uses a click detector to close and open a door. I used a for loop to make the door closing/opening look more smoother. The script is fine when it comes to opening the door, however the second part of the function, which allows for the closing of the door does not work. The button color does not change and stays at green, and the door does not close.
Button = script.Parent.Button Door = script.Parent.Door IsOpen = false function clicked() if IsOpen == false then Button.BrickColor = BrickColor.Green() for i = 0, 1, 0.1 do wait() Door.Transparency = i Door.CanCollide = false end IsOpen = true elseif IsOpen == true then Button.BrickColor = BrickColor.Red() for i = 1, 0, 0.1 do wait() Door.Transparency = i Door.CanCollide = true end IsOpen = false end end Button.ClickDetector.MouseClick:connect(clicked)
Your second for loop is trying to count from 1 to 0 by adding .1. You need to subtract .1. Here's the new code. ~~~~~~~~~~~~~~~~~ Button = script.Parent.Button Door = script.Parent.Door IsOpen = false
function clicked() if IsOpen == false then Button.BrickColor = BrickColor.Green() for i = 0, 1, 0.1 do wait() Door.Transparency = i Door.CanCollide = false end IsOpen = true elseif IsOpen == true then Button.BrickColor = BrickColor.Red() for i = 1, 0, -0.1 do wait() Door.Transparency = i Door.CanCollide = true end IsOpen = false end end
Button.ClickDetector.MouseClick:connect(clicked)
~~~~~~~~~~~~~~~~~