KeyDown() Help

Hello all, im startin to become a frequent poster lol but i ran into some trouble!

Im making this script that says what my guy is doing, either walkin sprintin or jumpin.



function movement( ply )

        if ply:KeyDown( IN_FORWARD ) == true  then
                print("Forward.
")
        elseif ply:KeyDown( IN_JUMP ) == true then
                print("Jumped.
")
        elseif ply:KeyDown( IN_SPEED ) == true then
                print("Sprint.
")
        end


end


My code is in my init.lua and i have no errors.

Thanks for all the help guys and gals!

You have to hook it into the KeyPress hook probably, is what you want, the arguments of which are a player object and an IN_KEYS.

After declaring your function, put this:

hook.Add(“KeyPress”, “nameOfHook”, movement)

Also, you don’t need "
" at the end of print statements unless you want one blank line after every print.

I like your avatar by the way. That looks like autumn trees in the background.

thanks, now it works :slight_smile: i made my avatar in photoshop, it was pretty simple haha

Don’t go away just yet, you’ve got redundancy going on in your condition blocks, which just adds one extra operation per comparison.
Logically b == true is equivalent to b. If b is true then true == true is true. If b is false then false == true is false.
I really think programming languages should start giving warnings when people do boolean comparisons with constants, you only get then with sloppy code as b == false is equivalent to not b.
[lua]function movement( ply )
if ply:KeyDown( IN_FORWARD ) then
print(“Forward.”)
elseif ply:KeyDown( IN_JUMP ) then
print(“Jumped.”)
elseif ply:KeyDown( IN_SPEED ) then
print(“Sprint.”)
end
end[/lua]

Sometimes I do it the way you’re saying not to do it, just for clarity’s sake.

Do I feel a benchmark coming on?

[editline]09:21AM[/editline]

Also to maybe help you understand what he just said, everything is true but false and nil.

I don’t see the point of testing to find a result that is naturally intuitive. When you perform a comparison it looks to see if there’s any metamethods for comparison, that’s a lookup on the object’s metatable, then if it can’t find that it does the primitive comparison, which compares the raw value. This is either comparing the reference or the raw value.
However that doesn’t matter as not performing the comparison will obviously make the program simpler and run faster.

This is true and as a matter of fact older versions of lua didn’t even have the true or false in it.
With Lua there may be valid times when multiple equally true results could be retuned. In which case it may be important that ‘true’ means one behaviour happened, such as perhaps if a value was in an array, however a similar object which could be used like it might return a table containing indexes of where the value is in the array.
However this is also irrelevant because it is clearly defined that KeyDown will return a boolean value so any comparisons with another boolean value are redundant.