Help with UserInputService

I’m trying to make a holding keybind that prints “Holding H” every 0.1 seconds you hold H for but nothing’s printing

im new to UserInputService so i’m trying to figure some things out

it’s a local script in startercharacterscripts

local UIS = game:GetService("UserInputService")

local player = game.Players.LocalPlayer

local holding = false

UIS.InputBegan:Connect(function(key)
	if key == Enum.KeyCode.H and holding == false then
		holding = true
		while holding == true do
			wait(0.1)
			print("Holding H")
			if holding == false then
				print("Stopped holding")
			end
		end
	end
end)

UIS.InputEnded:Connect(function(key)
	if key == Enum.KeyCode.H and holding == true then
		holding = false
	end
end)

I also tried using a repeat until loop for the printing and that printed nothing.

You need to change the following part of your if statements from:

key == Enum.KeyCode.H

to:

key.KeyCode == Enum.KeyCode.H

First of all, move the while loop out of the function, under the connections. You can also use UserInputService:IsKeyDown() i think.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.