Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How could i print something once a key is pressed twice?

Asked by 4 years ago

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?

0
Edited my answer. raid6n 2196 — 4y
0
Edited again. raid6n 2196 — 4y
0
I also edited my answer lol Master_Aaron 59 — 4y
0
omg. again raid6n 2196 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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!

0
Thanks! XxGhostBoy_HDxX 38 — 4y
0
One problem it works with all of the keys, not just the E when pressed a second time XxGhostBoy_HDxX 38 — 4y
Ad
Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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.

0
There is an error on line 9 --attempt to perform arithmetic (sub) on function-- XxGhostBoy_HDxX 38 — 4y
0
edited raid6n 2196 — 4y
0
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. XxGhostBoy_HDxX 38 — 4y
0
edited raid6n 2196 — 4y
View all comments (4 more)
0
So it detects the "time1" variable as an unknown global, and it errors the same thing as i said before. XxGhostBoy_HDxX 38 — 4y
0
Edited gain. raid6n 2196 — 4y
0
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 XxGhostBoy_HDxX 38 — 4y
0
Hm, try now. raid6n 2196 — 4y

Answer this question