Advertisement
RicoVR

Doors open and close script

Aug 25th, 2023
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 1 1
  1. Certainly! Here's a script that can animate doors in Roblox Studio when a player gets close:
  2.  
  3. ```lua
  4. -- Put this script in the door part's LocalScript
  5.  
  6. local door = script.Parent
  7. local openPosition = Vector3.new(0, 0, -5) -- Adjust the open position of the door
  8. local closePosition = Vector3.new(0, 0, 0) -- Adjust the closed position of the door
  9. local playerDetectionRange = 10 -- Adjust the range at which the player triggers the door
  10.  
  11. -- Function to animate the door
  12. local function animateDoor(open)
  13. if open then
  14. door:TweenPosition(openPosition, Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 1, true)
  15. else
  16. door:TweenPosition(closePosition, Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 1, true)
  17. end
  18. end
  19.  
  20. -- Function to detect if a player is nearby
  21. local function detectPlayer()
  22. local players = game.Players:GetChildren()
  23. local nearestPlayer = nil
  24. local nearestDistance = math.huge
  25.  
  26. for i = 1, #players do
  27. local character = players[i].Character
  28. if character and character:FindFirstChild("HumanoidRootPart") then
  29. local distance = (door.Position - character.HumanoidRootPart.Position).Magnitude
  30. if distance < nearestDistance then
  31. nearestPlayer = players[i]
  32. nearestDistance = distance
  33. end
  34. end
  35. end
  36.  
  37. return nearestPlayer
  38. end
  39.  
  40. -- Main loop to detect player and animate the door accordingly
  41. while true do
  42. local player = detectPlayer()
  43.  
  44. if player and (door.Position - player.Character.HumanoidRootPart.Position).Magnitude <= playerDetectionRange then
  45. animateDoor(true) -- Open the door if a player is close
  46. else
  47. animateDoor(false) -- Close the door if no player is close
  48. end
  49.  
  50. wait(0.1) -- Adjust the update frequency of the loop if needed
  51. end
  52. ```
  53.  
  54. Make sure to place this script inside the door part's LocalScript in Roblox Studio. Adjust the open and close positions of the door, as well as the player detection range, according to your game's requirements.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement