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

How come this keydown function isnt working?

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

✓ Accepted answer

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)

1 comment

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.

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