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

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

Asked by 8 years ago
Edited 8 years ago

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?

0
Why do you require the elseif statement? From how I'm looking at the script, it isn't necessary. :P TheeDeathCaster 2368 — 8y
1
Can you include the full script with the part you declare the variable value. User#5423 17 — 8y
1
Please supply the full script (Or at least the Click function) RubenKan 3615 — 8y
1
Wait, does this fire again at all? Like, is it used in a function, loop, etc..? TheeDeathCaster 2368 — 8y
0
I'm sorry for the lack of information, I updated it. TheEnderShot 14 — 8y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

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)
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. TheEnderShot 14 — 8y
Ad

Answer this question