Having problems with UserInputService

So the code down below used to work properly until now I realized it doesn’t even work.

	
UserInputService.InputBegan:Connect(function(input, gameProcessed)
		
	if not gameProcessed and input.KeyCode == Enum.KeyCode.E then
		planeEngineOn = not planeEngineOn
			
		while not gameProcessed and UserInputService:IsKeyDown(Enum.KeyCode.W) and wait() do
			print("true") -- doesn't print..
			updatePlaneSpeed(true)	
		end

		while not gameProcessed and UserInputService:IsKeyDown(Enum.KeyCode.S) and wait() do
			updatePlaneSpeed(nil)	
		end	
	end 
end)	
while not gameProcessed and UserInputService:IsKeyDown(Enum.KeyCode.W) and wait() do
			print("true") -- doesn't print..
			updatePlaneSpeed(true)	
		end

I tried debugging and seems like it didn’t fire at all. No errors in the output.

1 Like

Did you write this in a serverscript?

1 Like

Of course not. It’s a local script inside StarterGui.

1 Like

If I understand correctly you want pressing E will start / stop the engine. While the engine is ON, holding W and S will update plane speed. Correct?

I think you should move your while not gameProcessed and UserInputService:IsKeyDown(Enum.KeyCode.W) outside the E binding event, and also evaluate whether or not the engine is currently on. Maybe like this?

UserInputService.InputBegan:Connect(function(input, gameProcessed)
		
	if not gameProcessed and input.KeyCode == Enum.KeyCode.E then
		planeEngineOn = not planeEngineOn
	end
	-- move W binding here, also add `planeEngineOn` checks
	while not gameProcessed and planeEngineOn and UserInputService:IsKeyDown(Enum.KeyCode.W) and wait() do
		print("true") -- doesn't print..
		updatePlaneSpeed(true)
	end

	while not gameProcessed and planeEngineOn and UserInputService:IsKeyDown(Enum.KeyCode.S) and wait() do
		updatePlaneSpeed(nil)	
	end	
end)
1 Like

Now I understand why it doesn’t work. I actually forgot to close it!

1 Like