Input not working

Hello!
I was making an E to open door and whenever I press the key code nothing happens.

script

local Player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService") -- Defining the UserInputService.

local Gui = script.Parent.BillboardGui
local Obj = script.Parent.Table.MainPart
local Near = false

RunService.RenderStepped:Connect(function()
	if Player:DistanceFromCharacter(Obj.Position) < 25 then
		Near = true
	else
		Near = false
	end
end)

UserInputService.InputBegan:Connect(function(Input)
	if Near == true then
		local key = Input.KeyCode
		if key == Enum.KeyCode.E then
			print(key.."worked")
		elseif key == Enum.KeyCode.LeftBracket then
			print(key.."worked")
		end
	end
end)

Try removing the if Near Statment, if it still doesnt work than its the UserInput, otherwise its the RunService

I already tried that and it still does not work.

Edit: Theres been Mac problems and im on max so idk if this is an engine bug.

You don’t need a loop every frame, you can simply check the distance when the player presses ‘E’.

Loops should be the last thing you look to when programming,

CODE
local UserInputService = game:GetService("UserInputService")
local door = workspace.Door

local player = game:GetService("Players").LocalPlayer

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then
		return
	end
	
	if input.KeyCode == Enum.KeyCode.E and (player:DistanceFromCharacter(door.Position) < 25) then
		print("Open door!")
	end
end)

under if Near == true then (btw you can simply just type if Near then) place a print right there. If it doesn’t print then it’s most likely just your renderstepped function. A fix for that, would just be to use magnitude e.g

if (part.Position - otherpart.Position).magnitude < range then
Near = true
else
Near = false
end

He doesn’t need to use Magnitude since he’s using the DistanceFromCharacter function.

Thank you all, I thought it was just an engine bug. :slight_smile:

I prefer distance from character because it is easier to work with.

You’re clearly doing that wrong if it doesn’t work.

yes, I think I was suppose to put it in a bracket

the Roblox developer doesn’t give a lot of info on distance from character

Are there any errors from the above code?

The output does not print errors.