HePezi Posted October 9, 2017 Posted October 9, 2017 Hi! Im trying to create a script that calculates how long player have pressed a key. example: if player presses a key for 2 seconds, then it will trigger next function. Thanks for any replies.
vx89 Posted October 9, 2017 Posted October 9, 2017 (edited) You listen for key down and up events. Store start time (getTickCount() - time in milliseconds) on 'down'. And on 'up' check if current tick count is >= 2000 milliseconds (assuming more than 2 seconds is also ok - you can add some buffer zone, like between 2 and 3 seconds). UPDATE: Ah, you probably meant knowing two seconds has passed even before releasing the key. Then you simply call https://wiki.multitheftauto.com/wiki/SetTimer on the 'down' event. You have to be careful with timers though. If user clicks down many times during the 2 seconds, the old timers have to be canceled or ignored. Edited October 9, 2017 by vx89 1
DNL291 Posted October 9, 2017 Posted October 9, 2017 I think this server-side code should work: bindKey( player, key, "both", funcInput ) function funcInput ( player, key, keyState ) if key == "key" and keyState == "down" then if timer then return end timer = setTimer( f, 2000, 1, player ) --outputChatBox("down") elseif key == "key" and keyState == "up" then if timer and isTimer(timer) then --did not wait 2 seconds end --outputChatBox("up") end end function f( p ) killTimer(timer) timer = nil end 1
HePezi Posted October 10, 2017 Author Posted October 10, 2017 Thanks everyone! DNL291 your code was super useful. changed it a bit and now it works like a charm
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