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

Is there a way to break out of an if statement?

I have an inventory GUI that has 2 modes, those two modes are as follows:

craft: The materials that can be used when crafting.
build: The buildables that you can place on your plot

Now the script that I have that controls the button to toggle between craft and build mode contains the following:

script.Parent.MouseButton1Click:connect(function()
	local value = game.Players.LocalPlayer.PlayerGui.Inventory.mode.Value
	if value == "craft" then
		value = 'build'
		script.Parent.Text = "build"
	elseif value == "build" then
		value = 'craft'
		script.Parent.Text = "craft"
	end
end)

With the default state of the inventory being build mode, when I hit the button, it changes to craft mode, however when it is clicked again it does not switch back to build mode. I assumed the problem was when it ran through the script, it saw the value as craft then switched it to build, then the next elseif statement saw it as build and switched it back to craft, resulting in no change. I decided that if this was the problem than a possible solution would be to break out of the if statement before it could continue to the elseif statement. I have a couple questions:

If breaking from the if statement would be a valid solution to this problem, how could I break out of it and If breaking from the if statement wouldn't be a proper solution, what would be?

5 comments

0

Why do you require the elseif statement? From how I'm looking at the script, it isn't necessary. :P

1

Can you include the full script with the part you declare the variable value.

1

Please supply the full script (Or at least the Click function)

1

Wait, does this fire again at all? Like, is it used in a function, loop, etc..?

0

I'm sorry for the lack of information, I updated it.

1 answer

✓ Accepted answer

Your problem is how you're attempting to set the value. The value variable is a just a variable containing the mode's value prior to the if statement, so lines 4 and 7 will only change that variable to a string. This doesn't accomplish anything.

If you want to change the value object, you have to make the 'value' variable reference the actual value object. Then you can index the 'Value' property to set it.

script.Parent.MouseButton1Click:connect(function()
	local value = game.Players.LocalPlayer.PlayerGui.Inventory.mode
	if value == "craft" then
		value.Value = 'build'
		script.Parent.Text = "build"
	elseif value == "build" then
		value.Value = 'craft'
		script.Parent.Text = "craft"
	end
end)

1 comment

0

Well, my theory as to why it wasn't working couldn't have been more off, I guess you learn something new every day. This seems to have worked, thank you.

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