Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Certainly! Here's a script that can animate doors in Roblox Studio when a player gets close:
- ```lua
- -- Put this script in the door part's LocalScript
- local door = script.Parent
- local openPosition = Vector3.new(0, 0, -5) -- Adjust the open position of the door
- local closePosition = Vector3.new(0, 0, 0) -- Adjust the closed position of the door
- local playerDetectionRange = 10 -- Adjust the range at which the player triggers the door
- -- Function to animate the door
- local function animateDoor(open)
- if open then
- door:TweenPosition(openPosition, Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 1, true)
- else
- door:TweenPosition(closePosition, Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 1, true)
- end
- end
- -- Function to detect if a player is nearby
- local function detectPlayer()
- local players = game.Players:GetChildren()
- local nearestPlayer = nil
- local nearestDistance = math.huge
- for i = 1, #players do
- local character = players[i].Character
- if character and character:FindFirstChild("HumanoidRootPart") then
- local distance = (door.Position - character.HumanoidRootPart.Position).Magnitude
- if distance < nearestDistance then
- nearestPlayer = players[i]
- nearestDistance = distance
- end
- end
- end
- return nearestPlayer
- end
- -- Main loop to detect player and animate the door accordingly
- while true do
- local player = detectPlayer()
- if player and (door.Position - player.Character.HumanoidRootPart.Position).Magnitude <= playerDetectionRange then
- animateDoor(true) -- Open the door if a player is close
- else
- animateDoor(false) -- Close the door if no player is close
- end
- wait(0.1) -- Adjust the update frequency of the loop if needed
- end
- ```
- 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