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

ScreenGUI Button script ONLY works in Studio > test?

The script only works in Studio's test solo feature; not in-game.

The following code is located in a LocalScript:

function activate()
	
if script.Parent.Value.Value ==1 then
	
script.Parent.Value.Value =2


script.Parent.UI.Visible =true
script.Parent.UIcover.Visible =true
script.Parent.UIselection.Visible =true
script.Parent.OmnitrixOpen.Visible =false

script.Parent.ActivateSound:Play()





else if script.Parent.Value.Value ==2 then
	

script.Parent.Value.Value =3


script.Parent.UI.Visible =false
script.Parent.UIcover.Visible =false
script.Parent.UIselection.Visible =false
script.Parent.OmnitrixOpen.Visible =true

script.Parent.ScrollSound2:Play()




else if script.Parent.Value.Value ==3 then
	

script.Parent.Value.Value =4

script.Parent.TransformSound:Play()

script.Parent.OmnitrixGreenLight.Visible =true
script.Parent.LidGreenLight.Visible =true

script.Parent.OmnitrixGreenLight.ImageTransparency =1
script.Parent.LidGreenLight.ImageTransparency =1
wait (0.1)
script.Parent.OmnitrixGreenLight.ImageTransparency =0.75
script.Parent.LidGreenLight.ImageTransparency =0.75
wait (0.1)
script.Parent.OmnitrixGreenLight.ImageTransparency =0.5
script.Parent.LidGreenLight.ImageTransparency =0.5
wait (0.1)
script.Parent.OmnitrixGreenLight.ImageTransparency =0.25
script.Parent.LidGreenLight.ImageTransparency =0.25
wait (0.5)
script.Parent.OmnitrixGreenLight.ImageTransparency =0.25
script.Parent.LidGreenLight.ImageTransparency =0.25
wait (0.1)
script.Parent.OmnitrixGreenLight.ImageTransparency =0.5
script.Parent.LidGreenLight.ImageTransparency =0.5
wait (0.1)
script.Parent.OmnitrixGreenLight.ImageTransparency =0.75
script.Parent.LidGreenLight.ImageTransparency =0.75
wait (0.1)
script.Parent.OmnitrixGreenLight.ImageTransparency =1
script.Parent.LidGreenLight.ImageTransparency =1

script.Parent.OmnitrixGreenLight.Visible =false
script.Parent.LidGreenLight.Visible =false



else if script.Parent.Value.Value ==4 then
	

script.Parent.Value.Value =1

script.Parent.TransformSound:Play()

script.Parent.OmnitrixRedLight.Visible =true

script.Parent.LidRedLight.Visible =true

script.Parent.OmnitrixRedLight.ImageTransparency =1
script.Parent.LidRedLight.ImageTransparency =1
wait (0.1)
script.Parent.OmnitrixRedLight.ImageTransparency =0.75
script.Parent.LidRedLight.ImageTransparency =0.75

wait (0.1)
script.Parent.OmnitrixRedLight.ImageTransparency =0.5
script.Parent.LidRedLight.ImageTransparency =0.5

wait (0.1)
script.Parent.OmnitrixRedLight.ImageTransparency =0.25
script.Parent.LidRedLight.ImageTransparency =0.25

wait (0.5)
script.Parent.OmnitrixRedLight.ImageTransparency =0.25
script.Parent.LidRedLight.ImageTransparency =0.25

wait (0.1)
script.Parent.OmnitrixRedLight.ImageTransparency =0.5
script.Parent.LidRedLight.ImageTransparency =0.5

wait (0.1)
script.Parent.OmnitrixRedLight.ImageTransparency =0.75
script.Parent.LidRedLight.ImageTransparency =0.75

wait (0.1)
script.Parent.OmnitrixRedLight.ImageTransparency =1
script.Parent.LidRedLight.ImageTransparency =1


script.Parent.OmnitrixRedLight.Visible =false
script.Parent.OmnitrixOpen.Visible =false
script.Parent.LidRedLight.Visible =false










end
end
end
end
end










script.Parent.Activate.MouseButton1Click:connect(activate)


Do excuse my horrible script layout, gentlemen.

2 answers

If statements take the form of

if condition then
	outcome
else
	alternate outcome
end

As I'm sure you know, the condition will either be true or false. If it is true, the script will proceed to the original outcome, but if it is false, it will proceed to the alternate outcome. Whether or not the alternate outcome includes an if statement or not is irrelevant.

if condition then
	outcome
else
	--entire block is still 'alternate outcome';
	if condition then
		outcome
	end
end

It's the same as before; you have an outcome, and an alternate outcome. Both outcomes can have absolutely anything inside of them, but there are still essentially only two paths the script can take.

You cannot have more than one else in an if statement.

--INCORRECT;
if condition then
	outcome
else
	alternate outcome
else
	alternate outcome
end

Whether or not you have another if statement inside of those elses does not matter; there are only two possible paths the script can take, and what one of those paths include won't make a difference.


There is another keyword, however, that will allow for more outcomes; elseif.The elseif keyword is really very different from else if. It does not require any extra ends; it is still a part of the same if statement. They work simply; if the current condition is false, the script moves on to the next condition, until it finds one that is true. elseif takes the form of

if condition then
	outcome
elseif alternate condition then
	alternate outcome
elseif other alternate condition then
	other alternate outcome
end

With as many elseifs as needed.

If you just want two paths anyway, the following code is identical;

if condition then
	outcome
elseif alternate condition then
	alternate outcome
end

--EQUIVALENT TO;

if condition then
	outcome
else
	--alternate condition
	if condition then
		outcome
	end
end

However, if you need more than two main paths, you must use elseif. There can be more than one elseif, but only one else in an if statement.

2 comments

0

If you're implying that I replace all the "else if"s with "elseifs", it still doesn't work. And you haven't given me an overall view on what I should change / fix. Sorry if I come across as a bit 'rude' or 'narbish'.

0

I gave you the correct if statement formatting. Change else if to elseif and change the number of ends to the correct amount. Although there are things that could make the code more efficient and readable, if the paths are correct then that's the only functionality problem I can see.

function activate()
	if script.Parent.Value.Value ==1 then
		-- Code
	else
		if script.Parent.Value.Value ==2 then
			-- Code
		else
			if script.Parent.Value.Value ==3 then
				-- Code
			else
				if script.Parent.Value.Value ==4 then
					-- Code
				end
			end
		end
	end
end

script.Parent.Activate.MouseButton1Click:connect(activate)

This is what your code is implying. It contains very, very redundant characters and can be confusing.

A shorter way to execute the function but still can be executed the way you envisioned it is in this format using the convenient 'elseif' statement:

function activate()
	if script.Parent.Value.Value ==1 then
		-- Code
	elseif script.Parent.Value.Value ==2 then
		-- Code
	elseif script.Parent.Value.Value ==3 then
		-- Code
	elseif script.Parent.Value.Value ==4 then
		-- Code
	end
end

script.Parent.Activate.MouseButton1Click:connect(activate)

Not as messy. And all of that in two 'ends', rather than five.

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