How to check if a player pressed E next to a part?

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 want to see if a player pressed E next to a specific part.

  1. What is the issue? Include screenshots / videos if possible!

I don’t know how to check if a player pressed E next to a specific part.
And I don’t have any screenshots and videos.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried looking in Developer Forum but I couldn’t find the right topic that I need to solve this.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

2 Likes

You could use a Proximity Prompt for this, but, if you don’t want to, you could check the distance after they press it:

local uis = game:GetService("UserInputService")
local part = --define the part
local plr = game.Players.LocalPlayer

uis.InputBegan:Connect(function(input, typing)
    if not typing then
        if input.KeyCode == Enum.KeyCode.E then
            if plr.Character then
                if (plr.Character.HumanoidRootPart.CFrame - part.CFrame).Magnitude < 2 then
                    print("Close enough.")
                end
            end
        end
    end
end)
1 Like

I tried this but it’s didn’t work. :confused:

1 Like
local Part = game.Workspace:WaitForChild("TestPart")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local char = Player.Character



UIS.InputBegan:Connect(function(code)
	if code.KeyCode == Enum.KeyCode.E then
		if (char.HumanoidRootPart.Position - Part.Position).Magnitude < 15 then
			print(Player.Name.."pressed E next to a part!")
		end
	end
end)
1 Like

I tried this but it’s didn’t work. :confused:

That’s weird. I don’t see anything in that code which would prevent it from running. Are you getting any errors or warnings?

Are you using a local script or a normal script?

A LocalScript inside of a BillboardGui.

Put the LocalScript inside StarterGui or StarterPlayerScripts

why not just use a proximity prompt? this does exactly what you want

I’ve made my own proximity prompt to make it’s look more cool!