GetMarkerReachedSignal

I am trying to create an animation that plays when you press a key, pauses once it reaches a marker, and unpauses once you let go of the key being held down. It currently pauses, but only unpauses if the key is let go before the animation reaches the stopping point. I have prints everywhere, and the marker event that is fired by ‘Release’ isn’t firing. I can’t seem to find a reason for it to not work, since the other event gets disconnected.

local module = {}

local pressOrRelease = 1

local function resetSpeed(animation)
	while pressOrRelease == 1 do
		wait()
	end
	animation:AdjustSpeed(1)
end

function module.F(player, inputType)
	--Animation
	local animation = Instance.new('Animation')
	animation.AnimationId = 'rbxassetid://14140855498'
	animation = player.Character.Humanoid:LoadAnimation(animation)
	animation.Priority = Enum.AnimationPriority.Action4
	local animEvent
	local pos
	
	if inputType == 'Pressed' then
		animation:Play()
	end
	
	--Check event type
	if inputType == 'Pressed' then

		--Do move
		--print('client F pressed')
		
		pressOrRelease = 1
		
		animation:Play()
		animEvent = animation:GetMarkerReachedSignal('Hold'):Connect(function()
			print('speed stopped')
			animation:AdjustSpeed(0)
			
			resetSpeed(animation)
			animEvent:Disconnect()
		end)

	elseif inputType == 'Released' then
		
		--Do move
		--print('client F released')
		
		print('2')
		pressOrRelease = 2
		
		animEvent = animation:GetMarkerReachedSignal('Release'):Connect(function()
			print('animation stopped')

			animation:Stop()
			
			--Disconnect event
			animEvent:Disconnect()
		end)
	end
end

ty for the help

3 Likes

When the F function fires when the F key is released, the code for creating an animation runs again, so it’s not changing the animation from when the F key got pressed. You want to store the animation somewhere so when it gets Released, it uses that stored animation instead of a new one, you could do this by moving the creation of the animation out of the function.

2 Likes

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