How to make InputEnded only for certain inputs?

I am currently making a shooting system with arrow keys, it simply shoots an fireball in the a certain direction when pressing an arrow key. Everything works so far. Only problem is that I tried making it so that it keeps firing when the player holds the arrow key

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)

	if input.UserInputType == Enum.UserInputType.Keyboard then 
		--print("Keyboard user.")
		if input.KeyCode == Enum.KeyCode.Up or input.KeyCode == Enum.KeyCode.Down or input.KeyCode == Enum.KeyCode.Left or input.KeyCode == Enum.KeyCode.Right then
			--print("Key pressed: " .. input.KeyCode.Name)
			
			bool = true
			while bool == true do
				
				if not isTouched then
					isTouched = true
					hrp.CFrame = CFrame.lookAt(hrp.Position,dictionary[input.KeyCode.Name])
					FireDirection(dictionary[input.KeyCode.Name], plr.Head.Position, input.KeyCode.Name)
					wait(RESET_SECONDS)
					isTouched = false
				end
				
				userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
					bool = false
					
				end)
				if bool == false then
					break
				end
			end
			-- debounce
			
			
		end


	else if input.UserInputType == Enum.UserInputType.Touch then
			print("Mobile user")
		end
	end

end)

Here’s a video demonstrating the problem

It works perfectly well if the player doesn’t move at all. If the player starts moving with WASD. The script just execute userinputService.InputEnded since the player touched another key.

Is there a way to prevent that from happening? I want the player to still be able to shoot while moving with WASD keys.

Thank you in advance!

2 Likes

Why not put this in separately?

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)

	if input.UserInputType == Enum.UserInputType.Keyboard then 
		--print("Keyboard user.")
		if input.KeyCode == Enum.KeyCode.Up or input.KeyCode == Enum.KeyCode.Down or input.KeyCode == Enum.KeyCode.Left or input.KeyCode == Enum.KeyCode.Right then
			--print("Key pressed: " .. input.KeyCode.Name)
			
			bool = true
			while bool == true do
				
				if not isTouched then
					isTouched = true
					hrp.CFrame = CFrame.lookAt(hrp.Position,dictionary[input.KeyCode.Name])
					FireDirection(dictionary[input.KeyCode.Name], plr.Head.Position, input.KeyCode.Name)
					wait(RESET_SECONDS)
					isTouched = false
				end
				
				if bool == false then
					break
				end
			end
			-- debounce			
		end
	else if input.UserInputType == Enum.UserInputType.Touch then
			print("Mobile user")
		end
	end
end)


userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
    if input.UserInputType == Enum.UserInputType.Keyboard then 
		--print("Keyboard user.")
		if input.KeyCode == Enum.KeyCode.Up or input.KeyCode == Enum.KeyCode.Down or input.KeyCode == Enum.KeyCode.Left or input.KeyCode == Enum.KeyCode.Right then
	    bool = false
    elseif input.UserInputType == Enum.UserInputType.Touch then
	    print("Mobile user")
        bool = false
    end
end)
3 Likes

The problem is you didn’t add checks to the InputEnded event. Try my script, see if it works.

It works thank you for the explanation!

1 Like