Opening and Closing a box model with ProximityPrompt

In short, I’m trying to make a cardboard box model open and close with proximity prompt.

I got two free models from the tool box and took elements from each box that I wanted to retain. After making a hybrid of the two, I rigged the box to get it ready for the Animation Editor. From there, I created two animations: one to close the box and the other to open the box (I made an open animation and reversed it to create the close animation).

The box works perfectly as intended the first time it is closed or opened, but the open animation does not play on the second attempt.

Video of issue

Originally, only the open animation worked. After the open animation had finished, it would reset back to its closed position since the animation finished. To fix this, I created keyframe events to pause the animation near its end, so the box could retain its position.

After implementing this, the key frame events would keep firing on each proximity prompt trigger. To fix that, I stored each event locally in a variable and if the variable had an event, it would be disconnected before the event could be called again.

With all that, my close animation worked perfectly, but my open animation would break after the first time it played.

Here’s my script:

local ProximityPrompt = script.Parent.ProximityPrompt
local BoxHumanoid = script.Parent.Parent.Humanoid

local Tape = BoxHumanoid.Parent.Head
local BoxClose = script.BoxClose -- Close Animation
local BoxOpen = script.BoxOpen -- Open Animation

local openAnim = BoxHumanoid:LoadAnimation(BoxOpen)
local closeAnim = BoxHumanoid:LoadAnimation(BoxClose)
local animTrack, Opened, ActionText, ObjectText, tapeRemoved, tapeAdded, fullyOpened, fullyClosed


ProximityPrompt.Triggered:Connect(function(Player)
	-- [ INSTANTIATE VARIABLES ]
	Opened = ProximityPrompt.Opened
	ProximityPrompt.Enabled = false
	
	-- [ CHECK FOR BOX'S STATUS ] --
	if Opened.Value == true then -- If box is opened
		print("Closing Box")
		ActionText = "Open Box"
		ObjectText = "Packaged Box"
		animTrack = closeAnim
		Opened.Value = false
	elseif Opened.Value == false then -- If box is closed
		print("Opening Box")
		ActionText = "Close Box"
		ObjectText = "Unpackaged Box"
		animTrack = openAnim
		Opened.Value = true
	end
	
	-- [ UPDATE PROXIMITY ] --
	ProximityPrompt.ActionText = ActionText
	ProximityPrompt.ObjectText = ObjectText
	
	-- [ PLAY ANIMATION ] --
	animTrack:AdjustSpeed()
	animTrack:Play()
	
	
	-- [ DISCONNECT ANY EXISTING EVENTS ] --
	if tapeRemoved then
		tapeRemoved:Disconnect()
	end
	
	if tapeAdded then
		tapeAdded:Disconnect()
	end
	
	if fullyClosed then
		fullyClosed:Disconnect()
	end
	
	if fullyOpened then
		fullyOpened:Disconnect()
	end
	
	
	-- [ KEYFRAME EVENTS ] --
	tapeRemoved = animTrack:GetMarkerReachedSignal("TapeRemoved"):Connect(function()
		print("Tape Invisible")
		Tape.Transparency = 1
	end)

	tapeAdded = animTrack:GetMarkerReachedSignal("TapeAdded"):Connect(function()
		print("Tape Visible")
		Tape.Transparency = 0
	end)

	fullyOpened = animTrack:GetMarkerReachedSignal("FullyOpened"):Connect(function()
		print("BOX OPENED")
		animTrack:AdjustSpeed(0)
		ProximityPrompt.Enabled = true
		
	end)

	fullyClosed = animTrack:GetMarkerReachedSignal("FullyClosed"):Connect(function()
		print("BOX CLOSED")
		animTrack:AdjustSpeed(0)
		ProximityPrompt.Enabled = true
	end)
	
end)
ProximityPrompt.Enabled = false

The 2nd line of the callback function connected to the triggered event of the proximity prompt instance disables the proximity prompt and it’s never enabled again. Add the following line of code somewhere suitable so that the prompt is re-enabled or just remove the line of code which disables the prompt.

ProximityPrompt.Enabled = true

It is enabled again in my keyframe events. If you scroll down in the script, it’s listed there. I re-enable the proximity prompt once the animation reaches a certain keyframe. The proximity prompt works just fine. It’s just that the open animation seems to not play again after the first time.

Here’s the keyframe events that re-enable the proximity prompt (once again, it’s at the very bottom of the script):

fullyOpened = animTrack:GetMarkerReachedSignal("FullyOpened"):Connect(function()
		print("BOX OPENED")
		animTrack:AdjustSpeed(0)
		ProximityPrompt.Enabled = true
		
	end)

	fullyClosed = animTrack:GetMarkerReachedSignal("FullyClosed"):Connect(function()
		print("BOX CLOSED")
		animTrack:AdjustSpeed(0)
		ProximityPrompt.Enabled = true
	end)

Edit:
Here’s a video that shows my issue.

I managed to fix my issue after trying a slightly different approach. Playing the animations inside the if statements fixed my problem.

1 Like