How to add door opening animation?

  1. What do you want to achieve?
    I want to try and add a player animation when someone opens a door, can anyone help?

  2. What solutions have you tried so far?
    I tried adding a animation when the player opens the door but it doesn’t work and I don’t know what to do.

local Hinge = script.Parent.PrimaryPart
local opened = false

local Promt = script.Parent:WaitForChild("ProximityPrompt")

function OpenDoor()
	if opened == false then
		opened = true
		for i = 1, 21 do
			script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(5), 0))
			wait()
		end
	else
		opened = false
		for i = 1, 21 do
			script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(-5), 0))
			wait()
		end
	end
end
Promt.Triggered:Connect(function(Players)
	OpenDoor()
end)
script.Parent.Door1.ProxomityPrompt.Triggered:Connect(OpenDoor)

1 Like

You can use TweenService to create a door opening animation.

Fixed code:

--//Services
local TweenService = game:GetService("TweenService")

--//Variables
local Hinge = script.Parent.PrimaryPart
local Prompt = script.Parent:WaitForChild("ProximityPrompt")

--//Controls
local isOpened = false
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)

--//Functions
local function OpenDoor()
	if isOpened then
		--//Close door
		local doorTween = TweenService:Create(Hinge, tweenInfo, {
			CFrame = Hinge.CFrame * CFrame.Angles(0, math.rad(-100), 0)
		})

		doorTween:Play()
		doorTween.Completed:Wait()
	else
		--//Open door
		local doorTween = TweenService:Create(Hinge, tweenInfo, {
			CFrame = Hinge.CFrame * CFrame.Angles(0, math.rad(100), 0)
		})

		doorTween:Play()
		doorTween.Completed:Wait()
	end
	
	isOpened = not isOpened
end

Prompt.Triggered:Connect(OpenDoor)
script.Parent.Door1.ProxomityPrompt.Triggered:Connect(OpenDoor)
5 Likes

Nice, thank you so much! The code works.

1 Like

If it worked, please set my reply as the solution.

If you have any more questions, feel free to ask.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.