So basically, I have a door that when you press E, would open. But the problem is, I would be able to open it anywhere. How would I make it so that if I am within, let's say... 3 studs, I can open and close it?
✓ ANSWER ACCEPTED
How would I make my door so that you can only open it from a specific distance or less?
1 answer
✓ Accepted answer
Here's a way you could do it using magnitude
local UIS = game:GetService("UserInputService")
local checkrange = 3
local player = game.Players.LocalPlayer
local DoorOpen = false
UIS.InputBegan:Connect(function(key)
if (player.Character:WaitForChild("HumanoidRootPart").Position -
workspace.Door.Position).Magnitude <= checkrange and key.KeyCode == Enum.KeyCode.E then
if DoorOpen == false then
DoorOpen = true
--Open Door
else
DoorOpen = false
--Close Door
end
end
end)
This checks if the player is at max 3 studs away from the door, and if they press the letter E. If they do, you then put in what you want the door to do.
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
That would only be serversided though.
what u mean he posted a localscript
Yeah, this is a local script, because that’s what you detect user input on. You would need to fire an event to show the door opening on the server, but’s that’s up to you to do
I see. Thank you. Accepting answer.