Create an anchored Part named Door in Workspace. Add an Attachment named PromptAttachment to the door, place a ProximityPrompt inside the Attachment, and add a Script directly under the door.
Code: Select all
Workspace
└── Door (Part)
├── PromptAttachment (Attachment)
│ └── ProximityPrompt
└── ScriptConfigure the ProximityPrompt:
| Property | Value |
|---|---|
| ObjectText | Door |
| ActionText | Open |
| KeyboardKeyCode | E |
| MaxActivationDistance | 10 |
| HoldDuration | 0 |
Add the server script
Paste this code into the Script:
Code: Select all
local TweenService = game:GetService("TweenService")
local door = script.Parent
local attachment = door:WaitForChild("PromptAttachment")
local prompt = attachment:WaitForChild("ProximityPrompt")
local closedPosition = door.Position
local openPosition = closedPosition + door.CFrame.RightVector * (door.Size.X + 1)
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local isOpen = false
local busy = false
prompt.ActionText = "Open"
prompt.Triggered:Connect(function(_player)
if busy then
return
end
busy = true
local targetPosition = isOpen and closedPosition or openPosition
local tween = TweenService:Create(door, tweenInfo, {
Position = targetPosition,
})
tween:Play()
local playbackState = tween.Completed:Wait()
if playbackState == Enum.PlaybackState.Completed then
isOpen = not isOpen
prompt.ActionText = isOpen and "Close" or "Open"
end
busy = false
end)Test the door in Play mode. If it needs to slide in the opposite direction, subtract the direction vector instead:
Code: Select all
local openPosition = closedPosition - door.CFrame.RightVector * (door.Size.X + 1)Sources and verification
- Proximity prompts — Documents supported prompt parents, positioning prompts with Attachments, prompt text and input properties, activation distance, and zero-duration input behavior.
- ProximityPrompt — Documents the default E keyboard key, supported parents, text properties, activation distance, and the Player parameter supplied by Triggered.
- TweenService — Documents TweenService:Create and demonstrates tweening the Position of an anchored Part.
- CFrame — Documents orientation-derived CFrame vectors, including RightVector.