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

Why won't my KeyCode script work? UserInputService

Asked by 5 years ago

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)
0
its gameproceed not gameprocessed Gameplayer365247v2 1055 — 5y
0
Still didn't work PaliKai13 92 — 5y
0
GameProcessed is a variable. It can be whatever you want, you can even use _ and p, if you wished. Fifkee 2017 — 5y

2 answers

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago

Do the thing with the key held stuff.

```lua

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.

lua 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. lua 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)

0
Also wrong. DeceptiveCaster 3761 — 5y
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 :) Fifkee 2017 — 4y
Ad
Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago

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

0
Thank you, but I need a hold. Do you have any idea why my script won't work? PaliKai13 92 — 5y
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. Fifkee 2017 — 5y
0
userinputservice always glitches out for me so i use getmouse and it works perfectly Gameplayer365247v2 1055 — 5y

Answer this question