generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Detecting if a key is pressed twice

Previous Thread :: Next Thread 
thedeathmaster01 is not online. thedeathmaster01
Joined: 14 Mar 2010
Total Posts: 6331
30 Sep 2012 12:36 PM
So if I do

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.KeyDown:connect(function(key)
timespressed = Instance.new("IntValue", game.Players.LocalPlayer)
timespressed.Value = timespressed.Value + 1
if timespressed.Value == 2 then
print(key .. " was pressed twice")
end
end)

Would that detect if a key is pressed twice?
Report Abuse
blackboy167thst is not online. blackboy167thst
Joined: 22 Jul 2011
Total Posts: 2468
30 Sep 2012 12:38 PM
Yes, but you didn't say what the key is.
Report Abuse
L2000 is not online. L2000
Joined: 03 Apr 2008
Total Posts: 77448
30 Sep 2012 12:41 PM
The concept would work; however, you're inserting a new IntValue for each press. Instead, you should insert it outside of the KeyDown event, and when the key is pressed, increase the value.

local mouse = game.Players.LocalPlayer:GetMouse()
local keyToCheck = "w" -- Check for W pressed twice; change to key

-- Insert the value
timespressed = Instance.new("IntValue", game.Players.LocalPlayer)
mouse.KeyDown:connect(function(key)
if key:lower() == keyToCheck:lower() then -- Check that it's the same key
timespressed.Value = timespressed.Value + 1
if timespressed.Value == 2 then
print(key .. " was pressed twice")
end
end
end)
Report Abuse
thedeathmaster01 is not online. thedeathmaster01
Joined: 14 Mar 2010
Total Posts: 6331
30 Sep 2012 12:41 PM
Actually, after testing it just keeps inserting an IntValue into Player
Report Abuse
thedeathmaster01 is not online. thedeathmaster01
Joined: 14 Mar 2010
Total Posts: 6331
30 Sep 2012 12:42 PM
Thanks @L2000, but that only works once.
Report Abuse
doombringer42 is not online. doombringer42
Joined: 13 Nov 2007
Total Posts: 5445
30 Sep 2012 12:43 PM
mouse.KeyDown:connect(function(key)
local NextKey, waited = mouse.KeyDown:wait()
if key == NextKey and waited < 0.5 then -- if less than 0.5 seconds in between presses, and if the same key was pressed
print(NextKey .. " pressed twice")
end
end)

haven't tested, but here's proof of concept

¬ SHG Scripter Tier-2 ♣ LuaLearners Elite
Report Abuse
thedeathmaster01 is not online. thedeathmaster01
Joined: 14 Mar 2010
Total Posts: 6331
30 Sep 2012 12:43 PM
Oh I can figure that out. Thanks for the help!
Report Abuse
doombringer42 is not online. doombringer42
Joined: 13 Nov 2007
Total Posts: 5445
30 Sep 2012 12:44 PM
oh god, i grossly misinterpreted what you were asking for. lol

¬ SHG Scripter Tier-2 ♣ LuaLearners Elite
Report Abuse
thedeathmaster01 is not online. thedeathmaster01
Joined: 14 Mar 2010
Total Posts: 6331
30 Sep 2012 12:46 PM
@doom, attempt to compare nil with number
Report Abuse
thedeathmaster01 is not online. thedeathmaster01
Joined: 14 Mar 2010
Total Posts: 6331
30 Sep 2012 12:47 PM
On to L2000's script, could you make it so you must press it in a certain amount of time? Like doombringer had.
Report Abuse
thedeathmaster01 is not online. thedeathmaster01
Joined: 14 Mar 2010
Total Posts: 6331
30 Sep 2012 12:48 PM
Help?
Report Abuse
L2000 is not online. L2000
Joined: 03 Apr 2008
Total Posts: 77448
30 Sep 2012 12:48 PM
@Death
The script had a new IntValue each time you pressed the key; move the inserting part outside of the KeyDown event and it should work.

Alternatively, if you wanted to have it work for any key being pressed twice, you could use tables to hold when a key is pressed, and increase that key's value for each press:

local keysPressed = { }

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.KeyDown:connect(function(key)

if not keysPressed[key] then
-- Initialize the key value
keysPressed[key] = 0;
end

-- Increase the value by one
keysPressed[key] = keysPressed[key] + 1

-- Check the value
if keysPressed[key].Value == 2 then
print(key .. " was pressed twice")
end
end)

However, that's a bit more advanced; if you don't know tables yet, you can look at the Wiki:
http://wiki.roblox.com/index.php/Tables

Or you could use your current method for now, until you understand that.
Report Abuse
doombringer42 is not online. doombringer42
Joined: 13 Nov 2007
Total Posts: 5445
30 Sep 2012 12:49 PM
lol what i posted wasn't the full coding. you have to apply it into your code first.

my guess is that you didn't define 'mouse'. and i don't think my code does what you want.

¬ SHG Scripter Tier-2 ♣ LuaLearners Elite
Report Abuse
thedeathmaster01 is not online. thedeathmaster01
Joined: 14 Mar 2010
Total Posts: 6331
30 Sep 2012 12:51 PM
@L2000, I realised that after I posted my script. But I want it to see if the key has been pressed twice within 1 second.
Report Abuse
thedeathmaster01 is not online. thedeathmaster01
Joined: 14 Mar 2010
Total Posts: 6331
30 Sep 2012 12:51 PM
@doombringer, I did put it in the code and did define mouse. It just didn't work.
Report Abuse
XxChopSlaperxX is not online. XxChopSlaperxX
Joined: 09 Nov 2010
Total Posts: 3112
30 Sep 2012 12:52 PM
You still need help??

http://www.roblox.com/XxChopSlaperxX-39-s-Admin-Commands-GUI-item?id=93969383 -- Ceaselesssoul copied | Take one and enjoy! Read the description, has a lot of information.
Report Abuse
L2000 is not online. L2000
Joined: 03 Apr 2008
Total Posts: 77448
30 Sep 2012 12:52 PM
local mouse = game.Players.LocalPlayer:GetMouse()
local keyToCheck = "w" -- Check for W pressed twice; change to key
local maxSeconds = 0.5;
local done = false; -- Keep that as false; sets to true when pressed twice
-- Insert the value
timespressed = Instance.new("IntValue", game.Players.LocalPlayer)

local total = 0;

while not done do
local key, sec = mouse.KeyDown:wait()
total = total + sec;
if key:lower() == keyToCheck:lower() and total <= maxSeconds then
timespressed.Value = timespressed.Value + 1
if timespressed.Value == 2 then
print(key .. " was pressed twice")
-- End the loop
done = true;
end
elseif total > maxSeconds then
-- Reset
total = 0;
timespressed.Value = 0;
end
end

That should work for it; set maxSeconds to the maximum amount of time to keep a key (So, with 0.5, it would check that you pressed it twice within 0.5 seconds).
Report Abuse
thedeathmaster01 is not online. thedeathmaster01
Joined: 14 Mar 2010
Total Posts: 6331
30 Sep 2012 12:53 PM
Nevermind, I got it figured out. Thanks guys!
Report Abuse
thedeathmaster01 is not online. thedeathmaster01
Joined: 14 Mar 2010
Total Posts: 6331
30 Sep 2012 12:54 PM
@L2000, I figured it out, but thanks.
Report Abuse
doombringer42 is not online. doombringer42
Joined: 13 Nov 2007
Total Posts: 5445
30 Sep 2012 12:54 PM
game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
local NextKey, waited = mouse.KeyDown:wait()
if key == NextKey and waited < 0.5 then -- if less than 0.5 seconds in between presses, and if the same key was pressed
print(NextKey .. " pressed twice")
end
end)

put this in a localscript, it should work i think.

and also L2000, you never defined the 'Value' index for the keys...

¬ SHG Scripter Tier-2 ♣ LuaLearners Elite
Report Abuse
doombringer42 is not online. doombringer42
Joined: 13 Nov 2007
Total Posts: 5445
30 Sep 2012 12:56 PM
nevermind, ninja'd

¬ SHG Scripter Tier-2 ♣ LuaLearners Elite
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image