#How to wait for input

9 messages · Page 1 of 1 (latest)

next shuttle
#

I want to make a variable which goes back to 0 if the player has stopped pressing a key for more than "n" seconds

#

But will keep the variable value if the key is pressed within the timeframe

sleek tartan
#

you can probably store last key press to a variable

#

last key release*

#

and every heartbeat check the difference

#

this should work

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local lastRelease = 0
local isHolding = false
local MAX_TIME = 3 -- 3 seconds
UserInputService.InputEnded:Connect(function(input, gpe)
    if gpe then return end
    if input.KeyCode == Enum.KeyCode.E then
        lastRelease = os.clock()
        isHolding = false
    end
end)
UserInputService.InputBegan:Connect(function(input, gpe)
    if gpe then return end
    if input.KeyCode == Enum.KeyCode.E then
        isHolding = true
    end
end)

RunService.Heartbeat:Connect(function()
    if lastRelease ~= 0 and not isHolding and os.clock() - lastRelease > MAX_TIME then
        print("too much time passed!!")
    end
end)```
#

theres probably a simpler way but eh

next shuttle
cursive night
#

eh thats the most accurate way