Not able to disconnect a connection?

I have the following code running on the client:

        PlayerAnimation.Play(params.InitPlayer, "FlyingAttack")
        local stateConnection = humanoid.StateChanged:Connect(function(old, new)
            if (old == Enum.HumanoidStateType.Freefall) or (new == Enum.HumanoidStateType.Landed)then
                PlayerAnimation.Stop(params.InitPlayer, "FlyingAttack")
                stateConnection:Disconnect()
            end
        end)

When i run this code, I get the error: “attempt to index nil with ‘Disconnect’”

Further, for some reason the Roblox editor says stateConnection is an UnkownGlobal:

Any ideas why I cannot disconnect like this? How can I disconnect this event?

The variable need to exist before you can use it within a function

local stateConnection
stateConnection = humanoid.StateChanged:Connect(function(old, new)
....
1 Like