Detecting multiple animation events in a module script

When an animation is played, there are multiple animation events inside of it which I want the script to detect.
This method which I’m currently using does detect the first animation event, the problem is returning the event marker stops the function and therefore stopping the for loop so the next two events won’t be detected.

Local script

local MarkerSignal = animationTrack:PlayAnimation({"Hold", "DMG Begin", "DMG End"}) -- List of all the animation event names

Module script

function module:PlayAnimation(MarkerSignal)
	self.animationTrack:Play()
	
	if MarkerSignal then
		for _, marker in pairs(MarkerSignal) do
			self.animationTrack:GetMarkerReachedSignal(marker):Wait()
			
			return marker
		end
	end
end
1 Like

Doesn’t seem like anyone has any suggestions which is a shame.
Replacing return marker with a print function instead does print out all of the animation events, so I know my code is working, I just need a way of returning the marker value to the local script whenever an animation event is reached.

https://gyazo.com/442f52b8742b85a064373fa04a4f7104

Maybe the function can return a bindable event which is fired every time a new marker is reached:

function module:PlayAnimation(MarkerSignal)
	self.animationTrack:Play()
	local Bindable = Instance.new("BindableEvent")

	if MarkerSignal then
		for _, marker in pairs(MarkerSignal) do
			self.animationTrack:GetMarkerReachedSignal(marker):Once(function()
				Bindable.Event:Fire(marker)
			end)
		end
	end

	return Bindable.Event
end

In the local script, to test if this works:

local MarkerSignal = animationTrack:PlayAnimation({your markers})
MarkerSignal:Connect(print)

This should print out all of the markers

1 Like

When I meant print function, I meant print().
Besides that, your code doesn’t work, it doesn’t print out any of the animation events.
What I want is a method of returning these values whenever the animation event is reached in real time. Your example only returns the animation events at the end of the animation.

You could have the function accept a dictionary of functions that are called when the marker is reached:

function module:PlayAnimation(MarkerEventHandlerMap)
	if MarkerEventHandlerMap then
		for Marker, EventHandler in pairs(MarkerEventHandlerMap) do
		   self.animationTrack:GetMarkerReachedSignal(Marker):Connect(EventHandler) 
	    end
	end
	
	self.animationTrack:Play()
end

--and when your calling the function you would do:

animationTrack:PlayAnimation({
    ["Hold"] = function()
        --put the what you want to happen when this marker is reached
    end,
    
    ["DMG Begin"] = function()
    
    end
    --add the other markers and their respective handles here
})

would both of those parts u showed be in the same module script? I understand the logic but confused on how to exactly set it up. (This would be very useful for something I’m currently working on!)