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

Button clicks twice with this local script...can someone help me fix this?

Asked by
RGRage 45
8 years ago

I am trying to make a Gui that lets the player choose a class that they will play with for the rest of the game, but when I start the game in solo and click the left button on the gui it tweens to the next class and then when i think it is done it goes on to the next class.

Here is the script that I have written:

01function LeftToClass()
02    if LFrame:WaitForChild('CValue').Value == 'Paladin' then
03        if ChangingLeft == false then
04            if CV.Value == 'Huntsman' then
05                LeftToPaladin()
06            end
07        end
08    end
09    if LFrame:WaitForChild('CValue').Value == 'Rogue' then
10        if ChangingLeft == false then
11            LeftToRogue()
12        end
13    end
14    if LFrame:WaitForChild('CValue').Value == 'Huntsman' then
15        if ChangingLeft == false then
View all 26 lines...

The rest of the script to me is too big to put it one this question, but I will if anyone needs me to.

Any help is greatly appreciated.

1 answer

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

It likely has to do with your if statements. I don't know what the 'LeftTo[CLASS_NAME]' functions do, but I assume that they change LFrame's 'CValue'.


Lets examine this code:

01local value = 0
02 
03local function toggleValue()
04    if value == 0 then
05        value = 1
06    end
07    if value == 1 then
08        value = 0
09    end
10end
11 
12toggleValue()
13print(value) -- Outputs: 0

Why is the output 0? It started at 0 and we toggled the value, so what happened?

If we read the function line by line we ask "is value equal to 0?". Yes is is, so lets set it to 1. But then there is another if statement, so we ask "is value equal to 1?". Yes it is, because we just set it to that, so it gets switched back to 0.

Luckily the fix is easy, all we have to do is utilize else/elseif statements:

01local value = 0
02 
03local function toggleValue()
04    if value == 0 then
05        value = 1
06    elseif value == 1 then
07        value = 0
08    end
09end
10 
11toggleValue()
12print(value) -- Outputs: 1

And you'll notice the output is now what we expected.


As for your code, this seems to be the error that is occurring. To fix this utilize some elseif statements, as you only want it to switch once per click:

01LFrame.Button.MouseButton1Click:connect(function()
02    if ChangingLeft == false then
03        if LFrame:WaitForChild('CValue').Value == 'Paladin' then
04            LeftToPaladin()
05        elseif LFrame:WaitForChild('CValue').Value == 'Rogue' then
06            LeftToRogue()
07        elseif LFrame:WaitForChild('CValue').Value == 'Huntsman' then
08            LeftToHuntsman()
09        elseif LFrame:WaitForChild('CValue').Value == 'Sorcerer' then
10            LeftToSorcerer()
11        end
12    end
13end)
0
Thank you, I did not know that. At least I have learned something new! RGRage 45 — 8y
Ad

Answer this question