Double key pressed

Good day, so, I have a small problem, and I am trying to do a “slash” script for my game.
It should start when you press two times “W” in a small period of time.
However, I can not make it work.

local Player = game.Players.LocalPlayer
local Debounce = true

game:GetService("UserInputService").InputBegan:Connect(function(Input, Event)
	print("hey!")
	if Input.KeyCode == Enum.KeyCode.W and Debounce == true then
		Debounce = false
		wait(0.25)
		Debounce = true
		game:GetService("UserInputService").InputBegan:Connect(function(Input, Event)
			if Input.KeyCode == Enum.KeyCode.W and Debounce == true then
				print("two")
				Debounce = false
				local BV = Player.Character.HumanoidRootPart.BodyVelocity
				local R = Player.Character.HumanoidRootPart
				local T = (R.CFrame * CFrame.new(0,0,-200)) + Vector3.new(100,0,0)
				BV.Velocity = (T.p - R.CFrame.p).unit * 100
				BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
				--local playAnimation = Player.Character:WaitForChild("Humanoid"):LoadAnimation("None")
				Player.PlayerStats.Stamina.Value = Player.PlayerStats.Stamina.Value - 45
				--playAnimation:Play()
				wait(0.2)
				Player.Character.HumanoidRootPart.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
				Player.Character.HumanoidRootPart.BodyVelocity.MaxForce = Vector3.new(0, 0, 0)
				Debounce = true
				
			end
		end)
		wait(0.1)
		Debounce = true
		print("finished")
	end
end)

In this script, once you press once the “W”, it starts looping the second InputBegan event, so the first one does not work anymore.

1 Like

Use tick since you want the time since the last press. Your current method is just creating a new connection.

local last_press = tick()
local pressed = false -- Determines if the button was pressed before
UserInputService.InputBegan:Connect(function(input, engine_processed)
    if engine_processed then
        return
    end
    
    if input.KeyCode == Enum.KeyCode.W then
        pressed = true
    end

    if pressed and tick() - last_press < 1 then -- or whatever time you want
        -- Your code
        pressed = false -- After all your code, to reuse
        last_press = tick()
    end
end)
1 Like

Also update the value of last_press to tick() so you can reuse it

if pressed and tick() - last_press >= 1 then -- or whatever time you want
        -- Your code
        pressed = false -- After all your code, to reuse
        last_press = tick()
end
1 Like

Oops an oversight on my end, I fixed it thanks

It almost works, but it enables everytime. Like, when you press W once, and when around 2 seconds passes, you can still do the slash, but I want it to be like 0.5 gap time (more or less) between the double W pressed.

I tried adding

if input.KeyCode == Enum.KeyCode.W then
	pressed = true
	wait(0.5)
	pressed = false
end

but it does not let the next event go, and I do not know how to do that it can work.

Heres my way of doing it:

local LastTapped = false
UIS.InputBegan:Connect(function(inputObject, gameProcessed)
     if not gameProcessed then
          if not LastTapped then
				LastTapped = true
				wait(.25)
				LastTapped = false
			else
				--run code
			end
     end
end)
9 Likes