Help on animation becoming nil

I have the following snippet of code:

model.Parent = cam
local animLoaded = model.Animator:LoadAnimation(animUnloaded):Play()
print(animLoaded)
print(model)
print(model.Animator)
animLoaded:GetMarkerReachedSignal(animEvent):Connect(function()
	player.Character.HumanoidRootPart.CFrame = partCFrame
end)

However, animLoaded gets nil for some reason. Why does this happen? I know it’s nil because it errors at the marker event and it prints nil.

I’m not too sure about this, but I beleive it is because when you are loading the Animation, you are playing it as well. So when you are doing animLoaded:GetMarkerReachedSignal(animEvent):Connect(function()
you are actually doing model.Animator:LoadAnimation(animUnloaded):Play():GetMarkerReachedSignal(animEvent)

I would try this instead…

model.Parent = cam
local animLoaded = model.Animator:LoadAnimation(animUnloaded)
animLoaded:Play()
print(animLoaded)
print(model)
print(model.Animator)
animLoaded:GetMarkerReachedSignal(animEvent):Connect(function()
	player.Character.HumanoidRootPart.CFrame = partCFrame
end)

Oh well, it worked! I don’t know why that was happening for sure, but eh.

I can explain it for you…

So when you were loading the animation with this line of code:

local animLoaded = model.Animator:LoadAnimation(animUnloaded):Play()

You actually were Playing the Animation as well… You can’t actually use :GetMarkerReachedSignal() after :Play(). In the future when loading and playing an Animation, try and use two separate lines to load and play. It helps save confusion like this!