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.
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)
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
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.
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)