Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I make my door so that you can only open it from a specific distance or less?

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?

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.

4 comments

0

That would only be serversided though.

0

what u mean he posted a localscript

0

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

0

I see. Thank you. Accepting answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.