Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How come this keydown function isnt working?

Asked by 7 years ago

I have a surface gui pop up and then shrinks when you press e. It works the first time when i press e. So say I press e the first time and the gui pops up and then when i press e again it goes away. Then when i press e again it appears, but when i press e again then it doesnt go away... Help me please

mouse.KeyDown:connect(function(key)
    if keyDeb == false then keyDeb = true
        if key == "e" then
            spawnScreen()
        end
    elseif keyDeb == true then keyDeb = false
        wait(2)
        closeScreen()
    end
end)

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

First off, mouse.KeyDown is deprecated. Roblox recommends us to usehttp://wiki.roblox.com/index.php?title=API:Class/ContextActionService or http://wiki.roblox.com/index.php?title=API:Class/UserInputService instead.

However, when I look at your script, I can not find anything wrong with it functionally in solo mode. So it must have been a variable mistake or a mistake on the closeScreen() function

In the script below, I modified your script so that it works with UserInputService and all of the needed variables (excluding the screen functions):

local keyDeb = false
function KeyPressed(input, gameProcessedOnGUI)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local KeyPress = input.KeyCode
        if keyDeb == false and KeyPress == Enum.KeyCode.E then 
            keyDeb = true
          spawnScreen()
        elseif keyDeb == true then 
            keyDeb = false
            wait(2)
            closeScreen()
        end
    end
end
UserInput.InputBegan:Connect(KeyPressed)
0
I found the problem anyways. There was no such thing as onKeyDown. My mistake. Also thanks for the new way to do it. Im an old coding junkie so im not used to this new stuff yet. TickTockTheory 106 — 7y
Ad

Answer this question