So i want to make a dash script, and i need to know how do i make an function detect if a key is pressed 2 times. Let's say you must press E, 2 times to print something, how do i write that?
How could i print something once a key is pressed twice?
2 answers
Hey! So I'm not to sure how old/new you are to scripting so I'll run you through it like you're new. So first thing's first you're going to want to create a new LocalScript inside of StarterPlayer > StarterCharacterScripts. Then insert a "BooleanValue" inside of that LocalScript and name the value to "CanDash". When a new player joins the game this script will be parented to their character, which means the script will be put inside of their character.
Ok, now put a RemoteEvent into the ReplicatedStorage, a RemoteEvent basically lets the client (the player) talk and communicate with the server (the game) if you would like to read up more about RemoteEvents, (which I recommend if you don't know what they are since they are used in every single game) here is a link.
Once you've done that put this code inside of the LocalScript in StarterCharacterScripts
local UIS = game:GetService("UserInputService") -- Getting UserInputService
UIS.InputBegan:Connect(function(key) -- When any key is pressed run this code
if key.KeyCode == Enum.KeyCode.E then -- If the key the player pressed is E then run this code
if script.CanDash.Value ~= true then -- Checking too see if the CanDash value is true
script.CanDash.Value = true -- CanDash's value is true
wait(1)
script.CanDash.Value = false -- Then wait 1 second and set it to false
end
else
game.ReplicatedStorage.Dash:FireServer() -- Fire the RemoteEvent
end
end):
Almost done! Now put a Script inside of ServerScriptService and put this code inside of it:
game.ReplicatedStorage.Dash.OnServerEvent:Connect(function(player)
print("Whatever you want to print")
end)
And you're done! This should let the player click E two times within a 1 second time frame and if they do so, their walkspeed will double (you can make the dashing system more advanced I just focused on the core elements to the question.) If you have any questions or concerns just leave a reply and I'll try to get back to you as soon as possible!
2 comments
Thanks!
One problem it works with all of the keys, not just the E when pressed a second time
To detect if a key is pressed you could use this example could of InputBegan:
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.E then
print("E was pressed")
end
end)
Now, to detect if the player presses it 2 times, but we can't really just stack the code because it will only work once, so here is something I made:
local Keytimes = 0
game:GetService("UserInputService").InputBegan:Connect(
function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.E then
if Keytimes == 0 then
Keytimes = 1
print("Pressed Once")
elseif Keytimes == 1 then
Keytimes = 2
print("Pressed Twice")
end
end
end
)
This will work, but not as you expect it to, it won't work consecutively or repetitively. To fix this, we can use os.time to detect how long it took for the player to click again. (os.time is how many seconds since January 1st, 1970.)
Code:
local time1
local time2
local Keytimes = 0
game:GetService("UserInputService").InputBegan:Connect(
function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.E then
if Keytimes == 0 then
time1 = os.time()
Keytimes = 1
print("Pressed Once")
elseif Keytimes == 1 then
time2 = os.time()
if time2 - time1 <= 1 then
Keytimes = 0
print("Pressed Twice")
end
end
end
end
)
Accept the answer if it worked.
8 comments
There is an error on line 9 --attempt to perform arithmetic (sub) on function--
edited
So once i press E x2 it works, but if i press it a third time, it prints on the first E, i want it to end after 2 E's and start over.
edited
So it detects the "time1" variable as an unknown global, and it errors the same thing as i said before.
Edited gain.
it works, but the timer that is supposed to time the first click doesn't work, i want it to have a 1 sec timer until it resets and you can't press E the second time, only if you press it within this 1 second, it will work
Hm, try now.
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
Edited my answer.
Edited again.
I also edited my answer lol
omg. again