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

What is wrong with this script?

local Open = false

function Click() 
	wait()
	if Open == false then
		Open = true
		script.Parent.Parent.Open.Transparency = "0"
		script.Parent.Parent.Closed.Transparency = "1"
	end
	wait()
	if Open == true then
		Open = false
		script.Parent.Parent.Open.Transparency = "1"
		script.Parent.Parent.Closed.Transparency = "0"
	end
	wait()
end 

script.Parent.ClickDetector.MouseClick:connect(Click)

when I click, the parts change, but immediately change back to the starting position

2 answers

✓ Accepted answer

"Elseif" is a term used in if statements to define what will happen if the conditions of the first if statement are not met. The problem here is that you do not use elseif! You use if twice, making to if statements! The reasons this is the problem is because when the script runs, it will check if open is false. If it is false, then it will make open true. But then it runs the second if statement, because the script considers the two if statements as separate! By the time the second if statement is run, open is true, and since open is true, the second if statement makes open false. It will always become false again. An easy way to fix that is to change "if" to "elseif".

local Open = false

function Click() 
    wait()
    if Open == false then
        Open = true
        script.Parent.Parent.Open.Transparency = 0 --You don't need strings to define number values
        script.Parent.Parent.Closed.Transparency = 1
    wait()
    elseif Open == true then --if open isn't true, the script will skip this statement
        Open = false
        script.Parent.Parent.Open.Transparency = 1
        script.Parent.Parent.Closed.Transparency = 0
    end --if statements with elseif or else will only need one end
    wait()
end 

script.Parent.ClickDetector.MouseClick:connect(Click)

If you found my answer helpful, you can upvote and accept it. If you have any questions, just ask and I will edit my response accordingly.

Your problem is that you have two separate if statements. Scripts are read from left to right, top to bottom. It will therefore check both if statements separately, and because of the way you have it set up, both their conditions will be true and therefore they will both run.

Just use a single if statement and an elseif.

It is also rather pointless to delay the script a 30th of a second, it is unnoticeable and doesn't really help anything.

local Open = false

function Click() 
	if Open == false then
		Open = true
		script.Parent.Parent.Open.Transparency = "0"
		script.Parent.Parent.Closed.Transparency = "1"
	elseif Open == true then
		Open = false
		script.Parent.Parent.Open.Transparency = "1"
		script.Parent.Parent.Closed.Transparency = "0"
	end
end 

script.Parent.ClickDetector.MouseClick:connect(Click)

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