Something like this works:
local UserInputService = game:GetService("UserInputService")
local functionId = 0
local function DoStuff(id)
if id ~= functionId then return end
print("Successfully ran function")
end
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.T then
local currentId = functionId + 1
functionId = functionId + 1
delay(2, function()
DoStuff(currentId)
end)
end
end
end)
The idea is you keep track of an id variable which gets passed in to your function. Every time the delay is triggered, you increment the id. Then in the DoStuff function, if the passed in id doesn't match the current functionId, exit out. This way, every time you call the delay before the last one fires, it acts as if the timer is reset, as only the last call to delay will activate.
2 comments
Sorry, my wording wasn't clear enough, I need the button to prevent the function from being fired by resetting the delay timer back to 5 seconds.
Just updated my answer to suit your needs.