Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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?

3 comments

0

shouldn't you have gui=script.Parent and Player:GetMouse()

0

Can you use code block it helps ppl read the code.

0

Delete what you have and paste the code in a code block, then I'll help you

1 answer

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.