Fix UserInputService script issue

Hi DevForum! I was using the UserInputService and trying to track when a key is held, but for some reason isn’t working. There’s no errors; here is my script:

local dashnumber = 0
local qkeyheld = false
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character

uis.InputBegan:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.Q then
			qkeyheld = true
		end
	end
end)

while qkeyheld do
	print(" q key held")
	dashnumber = dashnumber + 1
	wait()
end

uis.InputEnded:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.Q then
			qkeyheld = false
		end
	end
end)

I think it would be better if you made dashnumber an Attribute and managed it. But anyways, Try changing the code above to the one below and let me know if it works.

task.spawn(function()
	while qkeyheld do
		print("q key held")
		dashnumber = dashnumber + 1
		task.wait()
	end
end)

Thank you for your reply, I tried it but surprisingly doesn’t work. Is it an issue with UserInputService? Because I never had this issue.

You are using your while statement before you declare your inputended statement. Try rearranging your code to look like this:

local dashnumber = 0
local qkeyheld = false
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character

uis.InputBegan:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.Q then
			qkeyheld = true
		end
	end
end)

uis.InputEnded:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.Q then
			qkeyheld = false
		end
	end
end)

while qkeyheld do
	print(" q key held")
	dashnumber = dash number + 1
	wait()
end

This has always seemed to work for me when I’ve came into with an issue like this. Goodluck!

1 Like

Then, You should try using a RBXScriptConnection for it. Basically, Whenever you press the Q Key, The Connection is Connected, It will start running every frame. (If we use RunService for it) And whenever we stop pressing it, It will stop running and will be Disconnected.

Try changing your code to this and let me know if it works!

-- Services
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

-- Player
local LocalPlayer = Players.LocalPlayer
local LocalCharacter = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

-- Variables
local DashNumber = 0
local HoldConnection = nil

-- Input Start
UserInputService.InputBegan:Connect(function(Input, IsTyping)
	if not IsTyping then
		-- Are we not Typing?
		if Input.KeyCode == Enum.KeyCode.Q then
			-- Is the KeyCode Q?
			print("Q Key Pressed!")
			-- Start our Connection:
			HoldConnection = RunService.Heartbeat:Connect(function()
				DashNumber += 1
				print("New DashNumber ->", DashNumber)
			end)
		end
	end
end)

-- Input End
UserInputService.InputEnded:Connect(function(Input, IsTyping)
	if not IsTyping then
		-- Are we not Typing?
		if Input.KeyCode == Enum.KeyCode.Q then
			-- Is the KeyCode Q?
			print("Q Key Stopped being Pressed!")
			-- Disconnect our Connection:
			if HoldConnection then
				HoldConnection:Disconnect()
				HoldConnection = nil
				warn("Disconnected!")
			end
		end
	end
end)

Similar to your issue.