UserInputService not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like to be able to get when the player is touching the part AND clicking “E”

  2. What is the issue? Include screenshots / videos if possible!
    It doesn’t give me the print as if the player has never clicked E.

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

local function Clicked()
end
UserInputService.InputBegan:Connect(function(key)
	print ("Try")
	if key.KeyCode == Enum.KeyCode.E then
		Part.Touched:Connect(Clicked)
	end
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Found nothing
local UserInputService = game:GetService("UserInputService")
local Part = script.Parent

local function Clicked(otherPart)
  print("Clicked")
end

UserInputService.InputBegan:Connect(function(input)
  if input.KeyCode == Enum.KeyCode.E then
    Part.Touched:Connect(Clicked)
  end
end)

1 Like

@DevHue Still not working with this

Wait, LOL I didn’t even paste the fixed code over. Oh well, I deleted it and I’m lazy my bad man.

From what I understood I assume you want to check if the player is pressing E when your part is touched.

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

local function Clicked()
    if UserInputService:IsKeyDown(Enum.KeyCode.E) do
         --Code goes here
    end
end

Part.Touched:Connect(Clicked)

This is untested but it should work.

2 Likes
local UserInputService = game:GetService("UserInputService")

local Part : BasePart = script.Parent

local isTouching = false

Part.Touched:Connect(function() isTouching = true end)

Part.TouchEnded:Connect(function() isTouching = false end)

local function Clicked()

end

UserInputService.InputBegan:Connect(function(key)

print ("Try")

if key.KeyCode == Enum.KeyCode.E and isTouching == true then

end

end)

You can check if the touched part is a part of the player.

1 Like

Try this?

local uis = game:GetService("UserInputService")
local Part = workspace.Part -- put the part here

local function istouching(part)
    for _, part in pairs(part:GetTouchingParts()) do
        if part:IsDescendantOf(game.Players.LocalPlayer.Character) then
            return true
        end
    end
end

uis.InputBegan:Connect(function(input)
    print("Pressed key", input.KeyCode.Name)
    if key.KeyCode == Enum.KeyCode.E and isTouching() then
        print("Pressed E, and touching part!")
    end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.