ManeXi Posted April 14, 2016 Share Posted April 14, 2016 (edited) 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 April 30, 2016 by Guest Link to comment
Simple0x47 Posted April 14, 2016 Share Posted April 14, 2016 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 Link to comment
Anubhav Posted April 14, 2016 Share Posted April 14, 2016 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
ManeXi Posted April 15, 2016 Author Share Posted April 15, 2016 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: 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: Link to comment
Anubhav Posted April 15, 2016 Share Posted April 15, 2016 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
Simple0x47 Posted April 15, 2016 Share Posted April 15, 2016 Ah just add local to sum = sum + 1 local sum = sum + 1 Link to comment
ManeXi Posted April 17, 2016 Author Share Posted April 17, 2016 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
Anubhav Posted April 17, 2016 Share Posted April 17, 2016 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
ManeXi Posted April 17, 2016 Author Share Posted April 17, 2016 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
ManeXi Posted April 24, 2016 Author Share Posted April 24, 2016 Anyone knows why it doesn't work? Please, this is important for me I would like to solve it. Link to comment
Moderators IIYAMA Posted April 29, 2016 Moderators Share Posted April 29, 2016 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
ManeXi Posted April 30, 2016 Author Share Posted April 30, 2016 Thanks alot IIYAMA it works perfectly, this can be set as Solved. Link to comment
Moderators IIYAMA Posted April 30, 2016 Moderators Share Posted April 30, 2016 Thanks alot IIYAMA it works perfectly, this can be set as Solved. Nice! Don't forget to check out this page, for scripters like you: https://forum.multitheftauto.com/viewtopic.php?f=108&t=98182 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now