Why is this logic statement wrong?

For ContextActionService it seems like whenever you press the key, the inputstate is Enum.UserInputState.Begin, and when you release the key, it is Enum.UserInputState.End. I want to ignore all states except Begin.

local CAS = game:GetService("ContextActionService")

local function bindAction(actionName, userInputState, inputObject)
	
	if not userInputState == Enum.UserInputState.Begin then return end
	print("What?") --This is triggering even though the input state is at end.
end

local keyCodes = {Enum.KeyCode.Q, Enum.KeyCode.W, Enum.KeyCode.E}

CAS:BindAction("Test", bindAction, false, unpack(keyCodes))

However, in the following line:

	if not userInputState == Enum.UserInputState.Begin then return end

It seems to ignore it completely, even when the input state is end. So when the input state is User.InputState.End, isn’t the statement suppose to be

if not false then return end

--Which equals this, but when its true it isn't returning end?
if true then return end

I am not entirely sure what I’m going wrong, can somebody explain why?

-- nvm

Make sure you include the wanted Enum to the Input instance when comparing input types

This is incorrect. The parameter userInputState is an Enum, and not an InputObject.

oh my u are right, i didn’t actually look at the code

Try this with the parentheses:

if not (userInputState == Enum.UserInputState.Begin) then return end

I think that not setting any parentheses works the same as the following

if (not userInputState) == Enum.UserInputState.Begin then return end

not userInputState is equal to false because userInputState isn’t nil, so you are essentially checking if false == Enum... which is always false.

4 Likes

Iam positive this is the right solution, since I had also experienced this issue, and by adding paranthesis, it works like a charm.

if userInputState ~= Enum.UserInputState.Begin then return end

Just wanted to offer this alternate solution.

2 Likes