What is wrong with my booleans?

UIS.InputBegan:Connect(function(Input,IsTyping)
	if IsTyping then 
		return
	elseif Input.KeyCode == Enum.KeyCode.Q then
         local Newstate = not SuitedUp        
		local CurrentState = SuitedUp
		
		SuitedUp = Newstate
		if CurrentState then
			SuitUp:FireServer()
		else
			SuitDown:FireServer()
		end
	
	end
end)

The issue here is that i have to activate the input twice before the script fires the function instead of once like i intended to, so I figured the issue lies with my booleans but i’m not exactly sure.

so u want the player to double tap q then the remoteevent will fire?

No, i want q to be tapped once before it fires. Right now I have to tap it twice before it does anything.

That is because “CurrentState” starts out as false, you change the NewState, but you don’t change the “CurrentState”, so after one press the current state stays as false. Try this:

UIS.InputBegan:Connect(function(Input,IsTyping)
	if IsTyping then 
		return
	elseif Input.KeyCode == Enum.KeyCode.Q then
         local Newstate = not SuitedUp        
		local CurrentState = Newstate
		
		SuitedUp = Newstate
		if CurrentState then
			SuitUp:FireServer()
		else
			SuitDown:FireServer()
		end
	
	end
end)

It worked but could you explain the boolean thought process, booleans always cause my brian to get mangled up.

Yeah sure!!! This is your original script. The reason yours doesn’t work is that you set CurrentState = Suited Up before you change Suited Up. I make these mistakes all the time, what I would recommend is to use a ton of print statements to find the problem. When you see what is wrong, go step by step of what is going to happen at each line, and you will find the problem.

SuitedUp = false

--1 Click
NewState = not SuitedUp
therefore NewState = true

CurrentState = SuitedUp 
therefore CurrentState= false

SuitedUp = NewState
therefore SuitedUp = true

--if statement sees that CurrentState is false, fires SuitDows

--2 Clicks

NewState = not SuitedUp
therefore NewState = false

CurrentState = SuitedUp 
therefore CurrentState= true

SuitedUp = NewState
therefore SuitedUp = false


--if statement sees that CurrentState is true, it works this time

Both examples are the same lol?

No I was showing only your script, why click 1 doesn’t work but click 2 does work.

My Script:

--1 Click
NewState = not SuitedUp
therefore NewState = true

CurrentState = NewState
therefore CurrentState= true

SuitedUp = NewState
therefore SuitedUp = true

Oh but i still don’t get why the values are inverse the second round.

Look at my first example post, it shows what happens during the first click, and what happens during the second click.

Nvm ignore that reply of mine, I get it now. Thanks for the help man.

1 Like

No problem, glad I could help :slight_smile: .