local UIS = game:GetService("UserInputService")
local TapThreshold = 0.02
local KeyCode = Enum.KeyCode.E
UIS.InputBegan:Connect(function(Input,GPE)
if KeyPressed then return end
if GPE or Input.UserInputState ~= Enum.UserInputState.Begin then return end
if Input.KeyCode ~= KeyCode then return end
local Now = tick()
local HoldEventThread = task.delay(TapThreshold,function()
--This is your looped (or not) hold key code
end)
local InputEnded
InputEnded = UIS.InputEnded:Connect(function(Input)
if Input.KeyCode ~= KeyCode then return end
InputEnded:Disconnect()
task.cancel(HoldEventThread)
if tick() - Now < TapThreshold then
--This is your tap key code
end
end
end
You may have to fork and adjust something, maybe fixing some bugs as I can’t access studio currently.
You can use a boolean variable called “buttonHeld” and set it to false initially.
Use the InputBegan event to detect when the button is pressed. Inside the InputBegan event handler, set the “buttonHeld” variable to true.
Use the InputChanged event to continuously check if the button is still being held. Inside the InputChanged event handler, check if the “buttonHeld” variable is true. If it is, then the button is being held.
Additionally, you can use the InputEnded event to detect when the button is released. Inside the InputEnded event handler, set the “buttonHeld” variable to false
I made a code to try and it works for me, this is the code example:
local UserInputService = game:GetService("UserInputService")
local buttonHeld = false
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
buttonHeld = true
print("Key pressed: " .. input.KeyCode.Name)
end
end)
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if buttonHeld then
print("Key held: " .. input.KeyCode.Name)
end
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
buttonHeld = false
print("Key released: " .. input.KeyCode.Name)
end
end)
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local buttonHeld = {}
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
buttonHeld[input.KeyCode] = true
print("Key pressed: " .. input.KeyCode.Name)
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
buttonHeld[input.KeyCode] = false
print("Key released: " .. input.KeyCode.Name)
end
end)
RunService.RenderStepped:Connect(function()
for key, isHeld in pairs(buttonHeld) do
if isHeld then
print("Key held: " .. key.Name)
end
end
end)
If you want to print when the key is holding and what key is being held this is the perfect code