Hi,
i would like to check if a player is pressing 2 keys at once.
I already got this.
if ply:KeyDown( IN_USE ) and ply:KeyReleased( IN_FORWARD ) then
print("boi")
end
But i want it to run once.
How could i manage this? :/
It would be even better when i could do this in StartCommand hook cause i want to block movement when
the player is holding down the use button.
hook.Add( "StartCommand", "whatever2", function( ply, cmd )
if cmd:GetButtons(IN_USE) then
if cmd:GetButtons(IN_FORWARD) then
print("hm")
end
cmd:ClearMovement()
end
end)
But this is blocking the movement permanent :,(
[code]hook.Add("KeyPress", "RemoveProp", function(ply, key)
if key == IN_USE then
if ply:KeyDown(IN_SPEED) then
end
end
end[/code]
Didnt worked :<
if not ply:KeyDown( IN_USE ) and not ply:KeyReleased( IN_FORWARD ) then return false end
print("blah blah blah")
Sorry didn't see that you wanted to block movement, also just tested with a print statement and my code works and only executes once.
close but doesnt work too. :<
Its beeing triggered by IN_USE alone and the last pressed comb doesnt trigger.
Updated my code with freezing when holding the buttons.
(pre code update)
Hm, for me its only working when i press shift first. But thats not a real problem ^^
Then i just use shift instead of use xd
(after)
I got to something similier once.
But keyKeyPress and KeyRealease is beeing spammed :c
From my testing right now KeyPress only executes once but if you introduce KeyRelease and multiple buttons it seems to spam keypress and keyrelease one after the other.
Oh, yeah for me too. Keyrelease is flipping out.
It switches between all pressed keys.
Also ply:freeze fucks up the key hooks *cry*
Can you just do this?
hook.Add( "StartCommand", "whatever2", function( ply, cmd )
if cmd:GetButtons(IN_USE) then
cmd:ClearMovement()
end
end)
doesnt solve my problem.
I need an combination of 2 buttons to trigger an function once.
"It would be even better when i could do this in StartCommand hook cause i want to block movement when
the player is holding down the use button."
"I need an combination of 2 buttons to trigger an function once."
So you want it to allow only one move button, when use key is down?
Nah, i want to block all movement if use key is down.
While that i want to run an function once if an specific second button is beeing pressed.
If you still need a way to stop the player
local Player = FindMetaTable("Player")
// stops the movement of a player | eliminates velocity
function Player:StopMovement()
ply:SetVelocity(self:GetVelocity() * -1)
end
I'll take a look at the whole "key combo" shit later if you guys don't figure it out.
Maybe not the best way, but
local callFunc = true
hook.Add("StartCommand", "whatever2", function(ply, cmd)
if cmd:KeyDown(IN_USE) then
if cmd:KeyDown(IN_FORWARD) then
if callFunc then
print("yolo")
end
callFunc = false
else
callFunc = true
end
cmd:ClearMovement()
end
end)
Make up your god damn mind up, we're people not robots.
If you want to only run it once -
local button_comb = {}
hook.Add("PlayerButtonDown", "trig_when_two_keys_down", function(ply, button)
if IsValid(ply) then
local key1 = 15
local key2 = 33
if button == key1 or button == key2 then
if !table.HasValue(button_comb, button) then
table.insert(button_comb, button)
else
button_comb = {}
end
end
if table.HasValue(button_comb, key1) and table.HasValue(button_comb, key2) then
// place your stuff here
button_comb = {}
end
end
end)
properly stop movement(I tested it and it works fine, place in server files) -
hook.Add("StartCommand", "block_movement", function(ply , cmd)
// gotta use this serverside due to trust problems
if ply:KeyDown(32) and ply:KeyDown(8) then
cmd:ClearMovement()
end
end)
Please use IN_ enums and not raw numbers.
It's good practice, but how often (and why) do they change at all? If a new enumeration is added then it should just go at the end of the pre-existing list, right? Just curious.
Does it matter? It's a bad idea regardless of whether the enum is ever changed. How often do you use IN number five hundred twenty four thousand two hundred and eighty eight? Do you know what that enum is? I doubt it. If you read my code and saw that number, what exactly would that tell you? Nothing, right? So now you have to go to the gmod wiki, look up the IN enums, look for IN number five hundred twenty four thousand two hundred and eighty eight and see what key it represents.
Or I could just use IN_ZOOM in my code. Which do you think is easier?
Sorry, you need to Log In to post a reply to this thread.