Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My double tap script isnt working?

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

enabled = true
Mouse.KeyDown:connect(function(key)
if not enabled then
        return
    end

if key == "s" then

local lastTapTime = tick()

local function onTap()
    if tick() - lastTapTime < 1 then
        print("double Click!")
    end
	
	onTap()
    lastTapTime = tick()
end
end
end)

I found this script on another scripting helpers question and I've attempted to modify it but I've failed!

It's meant to print "double Click!" when the 's' key is pressed twice in under 1 second.

Please help.

1 answer

✓ Accepted answer

##Problem

It helps people out a great amount if the code is tabbed properly, so they may know where all the ends and functions line up. In your script you have a misplaced end for a function that would make it never able to be called on line 21. Secondly, since you set the lastTapTime variable inside the if statement, the script would always say it is less than or equal to one because you check that immediately.


##Solution

All you will have to do is move the end and the variable. What I had done was moved the lastTapTime to the outside of the KeyDown function and set it equal to 0. And I moved the end after the if statement that would check if the key was pressed within a second.


#Final Script

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local lastTapTime = 0

enabled = true
Mouse.KeyDown:connect(function(key)
    if not enabled then
        return
    end

    if key == "s" then

        local function onTap()
            if tick() - lastTapTime < 1 then
                print("double Click!")
            end
        end

        onTap()
        lastTapTime = tick()
        
    end
end)

##Deprecation

I would advise trying to understand the UserInputService events and functions as KeyDown events are now deprecated in favor of them. Check out the UserInputService's InputBegan event and see if you can convert the script to that. KeyDown should not be used in new works and is right now only kept for compatibility purposes.


######Hopefully this answered your question. If so please hit the accept answer button. If you have any questions feel free to ask them in the comments section below.

This discussion is preserved as it was. New answers, comments, and votes are closed.