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

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

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:

function LeftToClass()
	if LFrame:WaitForChild('CValue').Value == 'Paladin' then
		if ChangingLeft == false then
			if CV.Value == 'Huntsman' then 
				LeftToPaladin()
			end
		end
	end
	if LFrame:WaitForChild('CValue').Value == 'Rogue' then
		if ChangingLeft == false then
			LeftToRogue()
		end
	end
	if LFrame:WaitForChild('CValue').Value == 'Huntsman' then
		if ChangingLeft == false then
			LeftToHuntsman()
		end
	end
	if LFrame:WaitForChild('CValue').Value == 'Sorcerer' then
		if ChangingLeft == false then	
			LeftToSorcerer()
		end
	end
end

LFrame.Button.MouseButton1Click:connect(LeftToClass())

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

✓ Accepted answer

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:

local value = 0

local function toggleValue()
	if value == 0 then
		value = 1
	end
	if value == 1 then
		value = 0
	end
end

toggleValue()
print(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:

local value = 0

local function toggleValue()
	if value == 0 then
		value = 1
	elseif value == 1 then
		value = 0
	end
end

toggleValue()
print(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:

LFrame.Button.MouseButton1Click:connect(function()
	if ChangingLeft == false then
		if LFrame:WaitForChild('CValue').Value == 'Paladin' then
			LeftToPaladin()
		elseif LFrame:WaitForChild('CValue').Value == 'Rogue' then
			LeftToRogue()
		elseif LFrame:WaitForChild('CValue').Value == 'Huntsman' then
			LeftToHuntsman()
		elseif LFrame:WaitForChild('CValue').Value == 'Sorcerer' then
			LeftToSorcerer()
		end
	end
end)

1 comment

0

Thank you, I did not know that. At least I have learned something new!

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