Input key for frame visibility not working

So I wanted to make it so that when a player is 15 studs close to the part I have set below and they pressed the E button then the frame would become visible. But for some reason it doesn’t seem to work,I’ve tried messing around with the code but nothing seems to work. Maybe I’m missing something simple.

The Code:

–Variables
local player = game.Players.LocalPlayer
local part = workspace.Part
local billboard = part.BillboardGui
local frame = game.StarterGui.ScreenGui.Frame
local maxDist = 15
local UIS = game:GetService(“UserInputService”)

while wait() do
if (part.Position - player.Character.HumanoidRootPart.Position).Magnitude <= maxDist then
billboard.Enabled = true
else
billboard.Enabled = false
end
end

UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
if billboard.Enabled == true then
frame.Visible = not frame.Visible
end
end
end
end)

Well first of all, you really need to format your code.
Secondly, can’t you use ProximityPrompts for this? and lastly, the problem in your code is that the while wait() do will occupy the thread and it won’t let it get to the lines under it.

So the solution would be to connect the function to the event first and then start the while loop

As @Subsz said, you should check out Proximity Prompts, they’re exactly what you need. Plus, the ProximityPromptService is great as it can handle all the prompts’ events from the server.