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?
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!
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.