Scripting help: Gradual door opening based on player input in Roblox game

I have a code that defines a function called playOpeningAnimation which takes a doorBody parameter as input. When this function is called, it sets a finish value based on the CFrame of the doorBody’s primary part and a rotation value of 120 degrees in the x-axis.

Then, the function executes a loop where it gradually moves the doorBody from its current position to the finish position using the Lerp method. The loop increments i by 0.07 until i reaches 0.5, which determines the speed of the door opening animation.

During each iteration of the loop, the doorBody’s CFrame is set to the new interpolated value using the SetPrimaryPartCFrame method. Finally, the wait() function is used to delay the animation by a small amount of time before the next iteration of the loop.

How can I make a door open gradually, based on how long the player holds down the interaction key (E in this case)? Can you provide an example Lua script to achieve this effect? I tried to implement with ProximityPrompt, but failed.

Here is a simple example that you can customize to your needs. Just make sure you have more than 0 as your HoldDuration on the proximityprompt.

local proximityPrompt = script.Parent.ProximityPrompt

local stop = false

proximityPrompt.PromptButtonHoldBegan:Connect(function(player)
	stop = false
	for i = 1, 10 do
		task.wait(.2)
		print(i) -- Or set your CFrame here
		if stop then
			break
		end
	end
end)

proximityPrompt.PromptButtonHoldEnded:Connect(function(player)
	stop = true
end)