UserInputService playing multiple times

This is an example code.

UIS.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.F then
		print("Pressed F")
		task.wait(2)
		print("Ended")
	end
end)

Let’s say I spam F 8 times. Pressed F will get printed 8 times but Ended will also get printed 8 times.
If I press F while the script is playing then the script doesn’t reset.
There will now be 8 scripts running at the same time.
That’s why Ended is printed out 8 times.
Is there a way to reset the script? (Make Ended print out once even though you pressed F multiple times)

One way to do it would be to make the code in a function at the beginning of your script where you declare vaiables. Then have a control variable that is either true or false. if the variable is false you will run the function and if its true you will not run the function. when the function is ran set the variable to true and when its completed you set the variable to false. im not that good at explaining so let me know if you have questions

As @svenska_kartongbit said keep track of the initial press in a variable.

Like below:

1 Like

Do you mean this?

local Bool = false
UIS.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.F and Bool == false then
		Bool = true
		print("Pressed F")
		task.wait(2)
		print("Ended")
		Bool = false
	end
end)

I forgot to say that I want Pressed F print out multiple times but Ended only once. So this won’t work.

1 Like

I’m pretty sure this is what I’m looking for but I don’t understand 1 thing.

local comboResetTimer -- store the previous start punching timer

What do you mean by this?

Its just a variable that is true or false depending on when the cooldown is completed of 1 second (timer.isRunning)

It also stores the amount of time in an OOP table.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.