How can I check if any player is near a part using magnitude, and detecting if they press E near it?

Hello, right now I am making a tool that you click on the floor to place, and anybody in the server can press E near it to collect whatever is inside. I got the placing down part done, and I know how to use magnitude, but I am confused on how I would check if any player on the server is close to the part, and detecting if anyone presses E near it.

1 Like

You can connect UserInputService.InputBegan and check if the player is within magnitude

userInputService.InputBegan:Connect(function(input, gameProccessed)
   if gameProccessed then return end -- they are typing, ignore it

   if input.KeyCode == Enum.KeyCode.E then
      -- check if the player is within magnitude here and if they are, do things
   end
end)
1 Like
local player = game.Players.LocalPlayer

local UIS = game:GetService("UserInputService") --used for detecting when keys pressed

local part = --define the part here
local maxdistance = --how far away they need to be 

UIS.InputBegan:Connect(function(input)
         if input.KeyCode = Enum.KeyCode.E then --they pressed e
                  print("They pressed E")
                  local magnitude = (player.Character.HumanoidRootPart.Position - part.Position).Magnitude
                 print(magnitude)
                 if magnitude is <= max distance then
                       --run your code
                 end
        end

end)
1 Like

the new ProximityPrompts are always an option

1 Like

Yeah I know about the UIS but would I fire to the players client when theyre in reach or?

I was thinking this, but magnitude can be messed with on the client

Sure I’ll read this, thanks!!!