Delay function every time an event happens

I want to have a function run X seconds after the event happens.

The event is will happen multiple times.

Every time the event happens, the delay needs to be reset to X.

Thank you in advance, If there’s a similar topic I’m sorry

2 Likes

Is the X in the post related to custom number?

It’s a variable number. I don’t know how many seconds yet so I just put X.

Try using the for function.

Example:

for i=X, 1, -1 do
    wait(1)
    i -= 1
end

But how would I update it every time the event happens.

Or simply just

wait(X)

This supports decimals.

This is basically what I’m trying to do, Except DoSomething only happens 5 seconds after the last OnKeyPressed. A player could be spamming the key, but only 5 seconds after the last key spammed the function should run

local function DoSomething()
   print("Im doing something!")
end

local function OnKeyPressed ()
   wait(5)
   DoSomething
end

Using variables that hold when the event should be fired may be the way to go.

I’m not sure how. Could you show me an example?

local timeToFire = math.huge
game:GetService("RunService").Stepped:Connect(function()
if game.Workspace.DistributedGameTime>timeToFire then
timeToFire = math.huge
   print("Im doing something!")
end
end)

local function OnKeyPressed ()
   timeToFire = game.Workspace.DistributedGameTime + 5
end

This isn’t what he wants since he wants it to only fire for the last key press.

local UserInputService = game:GetService'UserInputService'
local RunService = game:GetService'RunService'

local function DoSomething()
	print("Im doing something!")
end

local Connection
local function OnKeyPressed()
	warn'Key Pressed'
	if Connection then
		Connection:Disconnect()
	end
	
	local LastPress = os.clock()
	Connection = RunService.Heartbeat:Connect(function()
		print("Time left:", math.ceil(5 - (os.clock() - LastPress)))
		
		if os.clock() - LastPress > 5 then
			DoSomething()
			Connection:Disconnect()
			Connection = nil
		end
	end)
end

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		OnKeyPressed()
	end
end)

2 Likes

Ignore my previous post. I figured out what you actually wanted. It also doesn’t spam (only every second)

local timeUntilAction = nil
local eventActivated = false
local allowTimeChange = true

local function DoSomething()
    print("I'm doing something!")
end

local function OnKeyPressed()
    if (allowingTimeChange) then
	    timeUntilAction = os.time() + 5
	    print(5)
	    if (not eventActivated) then
		    eventActivated = true
		    while (not (timeUntilAction - os.time() <= 0)) do
			    wait(1)
			    print(timeUntilAction - os.time())
		    end
		    allowingTimeChange = false
		    DoSomething()
		    eventActivated = true
		    allowingTimeChange = true
	    end
    end
end

consoleoutput1

1 Like

wait a minute, I need to fix the script.

local function DoSomething(msg)
   print(msg)
end

local function OnKeyPressed ()
   wait(5)
   DoSomething("Im doing something!")
end

This does not work. I have a punching script. Every time you punch you should get a fighting stance but it will go after a while. Right now after punching once it does not work.