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)
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