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

OnPress Key script not working?

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!

1 comment

0

I have edited my answer and it should work. I think the error was because you were checking the key again.

1 answer

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)

4 comments

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.

0

Or you could not do that because I wanted to leave it in a way he'd understand still.

0

Thanks! But it still doesn't work :( I'm a bit conufsed :/ Hmm.

0

Just chill, dude. Only giving a scripting tip.

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