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

Why won't my KeyCode script work? UserInputService

Why won't the following code print or change the force? I can not figure it out. This script is a child of a part. For now, when I press E, it should print "E on," but it doesn't. Any Ideas? Thanks, PaliKai

game:GetService("UserInputService").InputBegan:Connect(function(Input, gameprocessed)
        if Input.KeyCode == Enum.KeyCode.E then
	        script.Parent.BodyForce.Force = Vector3.new(1000, 0, 0)
	        print("E on")
        end
	end)

3 comments

0

its gameproceed not gameprocessed

0

Still didn't work

0

GameProcessed is a variable. It can be whatever you want, you can even use _ and p, if you wished.

2 answers

Do the thing with the key held stuff.


local Array = {}; --array in which we'll be storing keys pressed in via the keycode name.
local Test = false
game:GetService('UserInputService').InputBegan:Connect(function(henlo, waffle) --why did I name these variables this way?
	if (not waffle) then -- if a GUI with modal isn't visible, then..
		henlo = henlo.KeyCode; --redefine to keycode
		Array[henlo.Name:upper()] = henlo.Name:upper() --supports multiple keys being pressed. (i think)
	end
end) --on key pressed, this will fire.

Do the thing with the release key stuff.

game:GetService('UserInputService').InputEnded:Connect(function(henlotwo) -- we don't need GameProcessedEvent because that will allow people to pull a sneaky on ya and hold a key and alt+tab or hwatever.
	henlotwo = henlotwo.KeyCode; --you know, the do.
	Array[henlotwo.Name:upper()] = nil; --take it out when not released
end)

Do the test thing do thing variable stuff.

if (Test) then
	game:GetService('RunService').RenderStepped:Connect(function(step)
		local str = ''
		for i, v in pairs(Array) do
			str = str..tostring(v)
		end
		print(str)
	end)
end
print(doot doot trumpet trumpet)

2 comments

0

Also wrong.

0

I doubt it. He wishes for his keys to be detected when held. This works fine. Don't downvote call someone's answer wrong without providing a reason. If mine is wrong, post your own solution :)

an easier method is this

local mouse = game.Players.LocalPlayer:GetMouse()
    
    mouse.KeyDown:Connect(function(key)
    if key.lower() == "e" then
    --what u want to happen in here
    end
    end)

this will only work for clicks and not holds as you are trying to script there i see, but if u want a click instead of a hold then you can go with this script

3 comments

0

Thank you, but I need a hold. Do you have any idea why my script won't work?

0

No. DO NOT use GetMouse. Easier doesn't always mean better. GetMouse is deprecated and it kinda sucks imho. It doesn't have quite the qualities that UserInputService does.

0

userinputservice always glitches out for me so i use getmouse and it works perfectly

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