KeyPressed/KeyReleased

hey
i need to execute a console command to 1 every time when the key “f” is pressed and to execute the same command to 0 when the key is released

so heres my code


local players = player.GetAll()
for _, player in ipairs( players ) do
   if( player:KeyReleased( KEY_F ) ) then
		RunConsoleCommand("Classic_ESP_Player", "0")
   end
end

local players = player.GetAll()
for _, player in ipairs( players ) do
   if( player:KeyPressed( KEY_F ) ) then
		RunConsoleCommand("Classic_ESP_Player", "1")
   end
end

i have no clue why but its wont work for me

That seems like a really odd way to do it. You should be using GAMEMODE.KeyPress and GAMEMODE.KeyRelease instead of using the player methods KeyPressed, KeyReleased which return true if the criteria is met that frame. I don’t understand why you’re running it inside a player.GetAll loop. Is this supposed to be ran on the server or client?

Anyways, what you want is probably something like this:


hook.Add( "KeyPress", "Something1", function( Player, Key )

    if ( Key == KEY_F ) then

        Player:ConCommand( "Classic_ESP_Player", "1" )

    end

end )

hook.Add( "KeyRelease", "Something1", function( Player, Key )

    if ( Key == KEY_F ) then

        Player:ConCommand( "Classic_ESP_Player", "0" )

    end

end )

U

Thanks will try that, I’m having some troubles with the add-on when I will fif it I wi’ll try it out