Jump to content

[Solved]Counting the times that a key is pressed...


ManeXi

Recommended Posts

So I would like to count how many times I press for example the key "h" in 5 seconds, this is a sh!t I have made;

addEventHandler( "onClientKey", root,  
function(button,press)  
    if (button == "h") and (press) then 
        setTimer( 
        function add (a) 
            local sum = 0 
            for i,v in ipairs(a) do 
            sum = sum + v 
            end 
        end 
        ,5000,1) 
        outputChatBox("You have pressed the key H "..sum.." times") 
    end 
end ) 

And after the 5 seconds It would be good if it puts in the Chat "You have pressed the key H X times"

Honestly I have no idea of making a key counter, don't kill me, thanks for reading.

Edited by Guest
Link to comment
    addEventHandler( "onClientKey", root, 
    function(button,press) 
        if (button == "h") and (press) then 
                if not sum then 
                   local sum = 0 
                end 
                sum = sum + 1 
            outputChatBox("You have pressed the key H "..sum.." times") 
             
            return sum 
        end 
    end ) 

It's a simple script, just add the timer and done :P

Link to comment
  
playersTime = {} 
playersTimer = {} 
  
addEventHandler( "onClientKey", root, 
function(button,press) 
    if (button == "h") and (press) then 
        if not(playersTimer[localPlayer]) then 
            playersTimer[localPlayer] = setTimer(function() end, 5000, 1) 
            return 
        end 
        if (isTimer(playersTimer[localPlayer])) then 
            playersTime[localPlayer] = playersTime[localPlayer] + 1 or 0 
            return 
        else 
            playersTimer[localPlayer] = nil 
            outputChatBox("You have pressed the key H "..playersTime[localPlayer].." times") 
            playersTime[localPlayer] = nil 
        end 
    end 
end ) 
  

Link to comment
    addEventHandler( "onClientKey", root, 
    function(button,press) 
        if (button == "h") and (press) then 
                if not sum then 
                   local sum = 0 
                end 
                sum = sum + 1 
            outputChatBox("You have pressed the key H "..sum.." times") 
             
            return sum 
        end 
    end ) 

It's a simple script, just add the timer and done

In yours it gives me this error:

mo0e62K.jpg

  
playersTime = {} 
playersTimer = {} 
  
addEventHandler( "onClientKey", root, 
function(button,press) 
    if (button == "h") and (press) then 
        if not(playersTimer[localPlayer]) then 
            playersTimer[localPlayer] = setTimer(function() end, 5000, 1) 
            return 
        end 
        if (isTimer(playersTimer[localPlayer])) then 
            playersTime[localPlayer] = playersTime[localPlayer] + 1 or 0 
            return 
        else 
            playersTimer[localPlayer] = nil 
            outputChatBox("You have pressed the key H "..playersTime[localPlayer].." times") 
            playersTime[localPlayer] = nil 
        end 
    end 
end ) 
  

And this other in yours:

nWsxEkl.jpg

Link to comment
  
  
playersTime = {} 
playersTimer = {} 
  
addEventHandler( "onClientKey", root, 
function(button,press) 
    if (button == "h") and (press) then 
        if not(playersTimer[localPlayer]) then 
            playersTimer[localPlayer] = setTimer(function() end, 5000, 1) 
            playersTime[localPlayer] = 0 
            return 
        end 
        if (isTimer(playersTimer[localPlayer])) then 
            playersTime[localPlayer] = playersTime[localPlayer] + 1 
            return 
        else 
            playersTimer[localPlayer] = nil 
            outputChatBox("You have pressed the key H "..playersTime[localPlayer].." times") 
            playersTime[localPlayer] = nil 
        end 
    end 
end ) 
  

Link to comment

Simple01 yours still not working but it doesn't care Anubhav's does, anyway now I've got another doubt, instead of setting a certain key like before, I would like to get the keys from a certain control of a player, I have tried this:

playersTime = {} 
playersTimer = {} 
  
addEventHandler( "onClientKey", root, 
function(button,press) 
    if (button == getBoundKeys("crouch")) and (press) then 
        if not(playersTimer[localPlayer]) then 
            playersTimer[localPlayer] = setTimer(function() end, 1000, 1) 
            playersTime[localPlayer] = 0 
            return 
        end 
        if (isTimer(playersTimer[localPlayer])) then 
            playersTime[localPlayer] = playersTime[localPlayer] + 1 
            return 
        else 
            playersTimer[localPlayer] = nil 
            outputChatBox("You have pressed the crouch Key "..playersTime[localPlayer].." times") 
            playersTime[localPlayer] = nil 
        end 
    end 
end ) 

But it doesn't get rightly the keys

Link to comment

It won't as getBoundKeys return a table.

  
playersTime = {} 
playersTimer = {} 
keys = {} 
  
addEventHandler( "onClientKey", root, 
function(button,press) 
    local ckeys = getBoundKeys("crouch") 
    if (ckeys) then 
        for i, v in ipairs(ckeys) do 
            keys[v] = true 
        end 
    end 
    if (keys[button]) and (press) then 
        if not(playersTimer[localPlayer]) then 
            playersTimer[localPlayer] = setTimer(function() end, 1000, 1) 
            playersTime[localPlayer] = 0 
            return 
        end 
        if (isTimer(playersTimer[localPlayer])) then 
            playersTime[localPlayer] = playersTime[localPlayer] + 1 
            return 
        else 
            playersTimer[localPlayer] = nil 
            outputChatBox("You have pressed the crouch Key "..playersTime[localPlayer].." times") 
            playersTime[localPlayer] = nil 
        end 
    end 
end ) 
  

Link to comment
It won't as getBoundKeys return a table.
  
playersTime = {} 
playersTimer = {} 
keys = {} 
  
addEventHandler( "onClientKey", root, 
function(button,press) 
    local ckeys = getBoundKeys("crouch") 
    if (ckeys) then 
        for i, v in ipairs(ckeys) do 
            keys[v] = true 
        end 
    end 
    if (keys[button]) and (press) then 
        if not(playersTimer[localPlayer]) then 
            playersTimer[localPlayer] = setTimer(function() end, 1000, 1) 
            playersTime[localPlayer] = 0 
            return 
        end 
        if (isTimer(playersTimer[localPlayer])) then 
            playersTime[localPlayer] = playersTime[localPlayer] + 1 
            return 
        else 
            playersTimer[localPlayer] = nil 
            outputChatBox("You have pressed the crouch Key "..playersTime[localPlayer].." times") 
            playersTime[localPlayer] = nil 
        end 
    end 
end ) 
  

Still not working and there is not debug error.

Link to comment
  • Moderators

Controls?

local playersTime = 0 
local playersTimer 
local secondsHold = 5 -- And after the 5 seconds It would be good if it puts in the Chat "You have pressed the key H X times" 
  
addEventHandler( "onClientKey", root, 
function(button, press) 
    local ckeys = getBoundKeys("crouch") 
    if ckeys and ckeys[button] and press then 
        if not isTimer(playersTimer) then 
            outputDebugString("start playersTimer") 
            playersTimer = setTimer(function()  
                outputChatBox("You have pressed the crouch key "..playersTime.." times") 
                playersTime = 0 
            end, secondsHold*1000, 1) 
        else 
            playersTime = playersTime + 1 
            outputDebugString("playersTime + 1 = " .. playersTime) 
        end 
    end 
end) 

Custom keys?

local playersTime = 0 
local playersTimer 
local secondsHold = 5 -- And after the 5 seconds It would be good if it puts in the Chat "You have pressed the key H X times" 
local whichKey = "h" 
  
  
addEventHandler( "onClientKey", root, 
function(button, press) 
    if whichKey == button and press then 
        if not isTimer(playersTimer) then 
            outputDebugString("start playersTimer") 
            playersTimer = setTimer(function()  
                outputChatBox("You have pressed the key " .. whichKey .. " " ..playersTime.." times") 
                playersTime = 0 
            end, secondsHold*1000, 1) 
        else 
            playersTime = playersTime + 1 
            outputDebugString("playersTime + 1 = " .. playersTime) 
        end 
    end 
end) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...