Trouble when scripting Animation, GetMarkerReachedSignal() doesn't execute in the right Keyframe

Hi, I had some troubles with my scripts, I was trying to make a script where if an animation hits certain keyframe it runs a bunch of functions, but for some reason when I was testing it, the functions were run at the 1st frame of the animation but not the event frame.
For example:
This is the function I try to run when the animation hits the determined keyframe:

local function PauseResume(Track) 
        if Track.Speed == 1 then
            Track:AdjustSpeed(0)
        else
            Track:AdjustSpeed(1)
        end
    end

And with this it runs properly:

    Track:GetMarkerReachedSignal("EventFrame"):Connect(function()
        PauseResume(Track)
    end)

But with this it runs at the 1st keyframe (not the keyframe I wanted it to run):

local function KeyframeEvent(Track,EventName,...)
        local Arg = table.pack(...)
        Track:GetMarkerReachedSignal(EventName):Connect(function()
            if Arg then
                for _, Functions in pairs(Arg) do
                    if typeof(Functions) == "function" then
                        Functions()
                    end
                end
            end
        end)            
    end
KeyframeEvent(Track,"EventFrame",PauseResume(Track))

Why does this happen?

Ayo, you’re calling your PauseResume function in your call to KeyframeEvent.

edit: Fixed Code:

local function KeyframeEvent(Track,EventName,...)
        local Arg = table.pack(...)
        Track:GetMarkerReachedSignal(EventName):Connect(function()
            if Arg then
                for _, Functions in pairs(Arg) do
                    if typeof(Functions) == "function" then
                        Functions(Track)
                    end
                end
            end
        end)            
    end
KeyframeEvent(Track,"EventFrame",PauseResume)
1 Like