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

Can anyone explain why my KeyDown script for a Screen GUI is not working?

Asked by 7 years ago

local Player = game.Players.LocalPlayer gui = .Parent Mouse = Player:GetMouse Open = False end function PressM(Key) if (key == "m") then if (Open == false) then gui.MenuPart.Visible = true Open = true elseif (Open == true) then gui.MenuPart.Visible = false Open = false end end end

Mouse.KeyDown:connect(PressM)

I don`t know why this is not working. Anyone have suggestions?

0
shouldn't you have gui=script.Parent and Player:GetMouse() blowup999 659 — 7y
0
Can you use code block it helps ppl read the code. User#5423 17 — 7y
0
Delete what you have and paste the code in a code block, then I'll help you iamnoamesa 674 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

Here you go, it was largely working, but there were just a few mistakes that I've fixed for you. You should have caught these yourself, though. Have you enabled script analysis and console output? If not, do. They're extremely useful for debugging scripts like yours. Also, PLEASE use a code block next time for your code.

The fixed code:

local Player = game.Players.LocalPlayer 
Mouse = Player:GetMouse()

gui = --pointer to your player's screenGui
Open = false 

function PressM(key) 
    if (key == "m") then 
        if (Open == false) then 
            gui.MenuPart.Visible = true 
            Open = true 
        elseif (Open == true) then 
            gui.MenuPart.Visible = false 
            Open = false
         end
     end
 end
Mouse.KeyDown:connect(PressM)

Explanation: All you had wrong was some parentheses missing from the GetMouse function, which you could have easily debugged yourself. Again, script analysis and console is a must! A possible second reason is that you may have put this into a script, instead of a localscript, which would break the code as soon as you started a server. If you haven't already, put it into a localscript, inside of either the player's starterScripts or starterGui.

Hope this helped!

Ad

Answer this question