Jump to content

Bindkey timer


gerlachmax123

Recommended Posts

what do u mean 5seconds? like hold it down for 5 seconds?

yes

get when a player presses down the key.

then output to chat that he needs to hold the key for 5seconds.

after that, setTimer for 5 seconds, and if the player lets go off the key before timer runs out, cancel the timer and output it to the player

Link to comment
what do u mean 5seconds? like hold it down for 5 seconds?

yes

get when a player presses down the key.

then output to chat that he needs to hold the key for 5seconds.

after that, setTimer for 5 seconds, and if the player lets go off the key before timer runs out, cancel the timer and output it to the player

Still wondering why would you use timers for that..

Link to comment

another way;

client:

press = 0 
function loeschen() 
    if getKeyState("lctrl") then 
            addEventHandler ( "onClientRender", root, zaa) 
    end 
end 
bindKey("lctrl", "down", loeschen) 
function zaa() 
    press = press + 1 
    if press == 5000 then 
        triggerServerEvent ( "doMyFunction", getLocalPlayer() ) 
        press = 0 
    end  
end 

server:

function greetingHandler ( ) 
    -- your code goes here 
end 
addEvent( "doMyFunction", true ) 
addEventHandler( "doMyFunction", getRootElement(), greetingHandler ) 

Link to comment

No.. That won't work properly.

Use bindKey and getTickCount:

local ticks = {} 
  
function BindKeyToPlayer() 
    if ( eventName == "onPlayerJoin" ) then 
        bindKey ( source, "lctrl", "both", Loeschen ); 
    else 
        for index, value in ipairs ( getElementsByType "player" ) do 
            bindKey ( value, "lctrl", "both", Loeschen ); 
        end 
    end 
end 
addEventHandler ( "onPlayerJoin", root, BindKeyToPlayer ); 
addEventHandler ( "onResourceStart", root, BindKeyToPlayer ); 
  
function Loeschen ( _player, _key, _state ) 
    if ( _state == "up" ) then 
        if ( ticks[_player] ) then 
            local newTick = getTickCount(); 
            if ( ( ticks[_player] - newTick ) < 5000 ) then 
                ticks[_player] = false; 
            end 
        end 
    elseif ( _state == "down" ) then 
        ticks[_player] = getTickCount(); 
        while ( true ) do 
            local newTick = getTickCount(); 
            if ( ( ticks[_player] - newTick ) == 5000 ) then 
                --put your code here 
                break 
            end 
        end 
    end 
end 

I'm not sure if there is any error in my code, I didn't debug it because I don't have MTA San Andreas installed.

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