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 3 years ago
local UIS = game:GetService("UserInputService")
local tool = script.Parent
local function OnInputBegan(input, player)
    if input.UserInputType == Enum.UserInputType.E then
        tool:Clone().Parent = player.Backpack
        print("E was pressed")
    end
end
UIS.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 3 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:

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

This should work:

local UIS = game:GetService("UserInputService")
local tool = script.Parent

local function OnInputBegan(input, GPE)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.E then
            tool:Clone().Parent = player.Backpack
            print("E was pressed")
        end
    end
end

UIS.InputBegan:Connect(OnInputBegan)

Hopefully this helps

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

Answer this question