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

OnPress Key script not working?

Asked by 9 years ago

Hi guys! Any ideas why this isn't working? (It is not opening the gui) thanks :)

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
gui = script.Parent.Menu.Frame
Open = false
function PressM(key)
    if (key == "m") then
        if (Open == false) then
            gui.Visible = true
            Open = true
        elseif Open == true then
            gui.Visible = false
            Open = false
        end
    end
end

Mouse.KeyDown:connect(PressM)



It is a localscript in the startergui!

0
I have edited my answer and it should work. I think the error was because you were checking the key again. FearMeIAmLag 1161 — 9y

1 answer

Log in to vote
3
Answered by 9 years ago

Roblox recently deprecated KeyDown. From now on you will have to use the UserInputService in order to detect key pressing or mouse clicks. It actually makes it more difficult now to detect key pressings but I have fixed your script so that it should work. If you have any further questions or this does not work, please let me know.

Player = game.Players.LocalPlayer
gui = script.Parent.Menu.Frame

game:GetService("UserInputService").InputBegan:connect(function (input, _)
    if input.KeyCode == Enum.KeyCode.M then
        if gui.Visible == false then
            gui.Visible = true
        else
            gui.Visible = false
        end
    end
end)
0
You could shorten that function to 3 lines of code by putting, 'gui.Visible = not gui.Visible' i.e., setting the Visible property to the opposite of the Visible property. Perci1 4988 — 9y
0
Or you could not do that because I wanted to leave it in a way he'd understand still. FearMeIAmLag 1161 — 9y
0
Thanks! But it still doesn't work :( I'm a bit conufsed :/ Hmm. jjwood1600 215 — 9y
0
Just chill, dude. Only giving a scripting tip. Perci1 4988 — 9y
Ad

Answer this question