Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
2

KeyDown not working?

Asked by 6 years ago

So, I'm trying to make a Gui Game, but the "onkeydown" isn't working, 38 means ^. Thats the key number. But it's not working, any help?

function onkeyDown(key) 
    if key == 38 then
        script.Parent.Position.Z = 200
    end
end

or

function onkeyDown(key) 
    if key == 38 then
        script.Parent.Position = {0.5, -50},{0, 200}
    end
end

Thanks!

5 answers

Log in to vote
3
Answered by
Aethex 255 Moderation Voter
6 years ago

You need to use string.byte to use the number of the key.

function onkeyDown(key) 

    if key:byte() == 38 then

        script.Parent.Position = UDim2.new(0.5, -50, 0, 200);

    end

end

Ad
Log in to vote
0
Answered by
Versimn 20
6 years ago
function onkeyDown(key)
    if tonumber(string.byte(key)) == 38 then
        script.Parent.Position = UDim2.new(.5,-50,0,200)
    end
end
PATHHERE.MouseButton1Click:connect(onkeyDown) --PATHHERE = path to the gui button

should work, idk if tonumber is required :P

Log in to vote
-1
Answered by
IcyEvil 260 Moderation Voter
6 years ago

from what I see in the comments you need to do this

function onkeyDown(key) 

    if key:byte() == "38" then

        script.Parent.Position = UDim2.new(0.5, -50, 0, 200);

    end

end

Log in to vote
-2
Answered by 6 years ago

if the key part works then this will

function onkeyDown(key) 
    if key == 38 then
        script.Parent.Position = UDim2.new(.5,-50,0,200)
    end
end

Log in to vote
-3
Answered by 6 years ago

Few mistakes. One:

function onkeyDown(key)
    if key ==  "38" then -- Quotation Marks
        script.Parent.Position = {0.5, -50},{0, 200}
    end
end

For two: you might want to pick another letter instead of "^" because you have to hold shift.

function onkeyDown(key)
    if key ==  "Letter goes here" then -- Quotation Marks and any letter you want.
        script.Parent.Position = {0.5, -50},{0, 200}
    end
end

Though, I'm not highly 100% sure it will work.

0
I meant the up arrow. Grenaderade 525 — 6y
0
Well, I don't know about the up arrow part. Maybe put "Up"? DesignerDavid 0 — 6y
0
I did J, but it's still not working, I saw this in a weapon and it worked. Grenaderade 525 — 6y

Answer this question