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