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

User Input Service script not working properly?

Asked by 4 years ago
1local UIS = game:GetService("UserInputService")
2local tool = script.Parent
3local function OnInputBegan(input, player)
4    if input.UserInputType == Enum.UserInputType.E then
5        tool:Clone().Parent = player.Backpack
6        print("E was pressed")
7    end
8end
9UIS.InputBegan:Connect(OnInputBegan)

Code does not work when I press E, it also does not go into the player's backpack.

1 answer

Log in to vote
0
Answered by 4 years ago

Ok, I think you got a bit mixed up with UIS.

  1. Make sure you're using a localscript! Very important as UIS only runs on the client

  2. The InputBegan event gives two parameters, the input and the game processed event, not the player. Just get the player through game.Players.LocalPlayer

  3. UserInputType detects what type of input the player is using, such as keyboard, mouse, touchscreen, etc. To sense if the user is pressing a specific key, change the UserInputType you're checking for to keyboard, then add the following code inside:

1if input.KeyCode == Enum.KeyCode.E then
2    --E is pressed
3end

This should work:

01local UIS = game:GetService("UserInputService")
02local tool = script.Parent
03 
04local function OnInputBegan(input, GPE)
05    if input.UserInputType == Enum.UserInputType.Keyboard then
06        if input.KeyCode == Enum.KeyCode.E then
07            tool:Clone().Parent = player.Backpack
08            print("E was pressed")
09        end
10    end
11end
12 
13UIS.InputBegan:Connect(OnInputBegan)

Hopefully this helps

0
your truly the best. Ponytails2017 83 — 4y
Ad

Answer this question