Hello, I need some help we UserInputService.
Here is my code:
and what I want is that when you hold down W, the variable should still increase. But it does not even if I hold down It only changes it once every press.
1 Like
Use a while loop. For example if you wanted it to increase every second you could just wait 1 second between each iteration
local UserInputService = game:GetService("UserInputService")
local down = false -- determines if pressed down
UserInputService.InputBegan:Connect(input, engine_processed)
if engine_processed then
return
end
if input.KeyCode == Enum.KeyCode.W then
down = true
while down do
positionY = positionY + 5
wait(1) -- or however long you want
end
end
end)
UserInputService.InputEnded:Connect(function(input, engine_processed)
if engine_processed then
return
end
down = false -- breaks the loop
end)
4 Likes
You could even use ContextActionService:BindAction()
local ContextActionService = game:GetService("ContextActionService")
local var = 5;
local bool = true
local function handle(name, state)
if name == "Holding key" then
if state == Enum.UserInputState.Begin then bool = true
while bool do
var += 1
print(var)
wait() -- or whatever time
end
else
bool = false
end
end
end
ContextActionService:BindAction("Holding key", handle, true, Enum.KeyCode.W)
2 Likes