Enum.UserInputState.End doesn't fire when it should

I’m trying to make a plane script that keeps firing a RemoteEvent that makes the plane go up until E is done being pressed. That works great, but it doesn’t make the Enum.UserInputState.End fire, it just prints Enum.UserInputState.Begin, even when I push other buttons.

Here’s my LocalScript:

local service = game:GetService("ContextActionService")

local function handleAction(actionName, inputState, inputObject)
	if actionName == "FlyUp" then
		if inputState == Enum.UserInputState.Begin then
			repeat
			print("ooh!")
			print(inputState)
			game.ReplicatedStorage.FlyUp:FireServer()
			wait(0.1)
			until inputState == Enum.UserInputState.End
		end
     end
-- some unimportant keybind stuff

InputState only changes once that is when the function is called, trying to check if the InputState gets changed like you did won’t work.

So then how would I detect when the key is done being pressed?

Try creating a variable that changes to true when the player releases the button.

inputstate won’t change, rather it will fire handleAction again with a new inputState variable. You need to move the loop to be contained in an asynchronous external function, and end the loop when you change a variable.

running = false

function run()
spawn(function() while running do
blah
end)
end

function handleActuion(blah)
if inputState blah then
running = true
run()
else
running = false
end
end

And check if the variable is true on the repeat loop.

@Ty_IsHomeSchooled this is what i meant:

local service = game:GetService("ContextActionService")

local inputEnded = false

local function handleAction(actionName, inputState, inputObject)
  if actionName == "FlyUp" then
    if inputState == Enum.UserInputState.Begin then
      repeat
      print("ooh!")
      print(inputState)
      game.ReplicatedStorage.FlyUp:FireServer()
      wait(0.1)
      until inputEnded == true
      inputEnded = false
    elseif inputState == Enum.UserInputState.End then
      inputEnded = true
    end
  end
-- some unimportant keybind stuff
3 Likes

@Giggio450BR and @JarodOfOrbiter I am trying both of your ways and seeing which one is more efficient.

2 Likes

It’s the same thing, but his doesn’t use an external function.