How to avoid this random error

I get this error:

LoadAnimation requires an Animation object - Client - Transition:15
17:50:33.911 Stack Begin - Studio
17:50:33.911 Script ‘ReplicatedStorage.Modules.Transition’, Line 15 - function Do

On the following script:

function module:Do(player,tp)
	local HRP = player.Character.HumanoidRootPart
	local partCFrame = tp.CFrame
	local UnloadedAnim = AnimFolder[math.random(0,#AnimFolder)]
	print(UnloadedAnim)
	local Event = "Teleport"
	
	GUI.Parent = player.PlayerGui
	local LoadedAnim = Model.Animator:LoadAnimation(UnloadedAnim)
	print(LoadedAnim)
	LoadedAnim:Play()
	LoadedAnim:GetMarkerReachedSignal(Event):Connect(function()
		player.Character.HumanoidRootPart.CFrame = partCFrame
	end)
	LoadedAnim.Stopped:Wait()
	GUI.Parent = script
end

What am I doing wrong? This happens 50% of the time. The other time, stuff work.

idk, try to use :WaitForChild(), for HumanoidRootPart, the error says that it couldn’t find the object that is needed to be animated so that’s the only thing i can think of

Line 15, friend, what does HumanoidRootPart has to do with this?

It has to do with the UnloadedAnim it’s supposed to be an Animation object, I am guessing that it’s not due to it 0 to the max amount of children of the AnimFolder instead it should be 1 to the max.

Unless that AnimFolder is not a table of the children of a folder, retrieve by :GetChildren() this should fix it.
So fix this; line 10:

local UnloadedAnim = AnimFolder[math.random(1, #AnimFolder)]

Else another issue could have to do with the Object inside of the folder not being an Animation Instance, so instead you could go through the folder until
So change the same line again to try and make it work.

local UnloadedAnim
while #AnimFolder > 0 do
   local n = math.random(1, #AnimFolder)
   UnloadedAnim = AnimFolder[n] and AnimFolder[n]:IsA("Animation") and AnimFolder[n]
   if not UnloadedAnim then table.remove(AnimFolder, n) end
end

If the AnimFolder has no single object which is an Animation instance then UnloadedAnim will be nill.

1 Like

Sorry for the late response, here is my full script (original):

local module = {}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AnimFolder = ReplicatedStorage:WaitForChild("Animations"):WaitForChild("Framework"):GetChildren()
local GUI = script.Framework
local Model = GUI.ViewportFrame.WorldModel.FrameworkModel

function module:Do(player,tp)
	local HRP = player.Character.HumanoidRootPart
	local partCFrame = tp.CFrame
	local UnloadedAnim = AnimFolder[math.random(0,#AnimFolder)]
	print(UnloadedAnim)
	local Event = "Teleport"
	
	GUI.Parent = player.PlayerGui
	local LoadedAnim = Model.Animator:LoadAnimation(UnloadedAnim)
	print(LoadedAnim)
	LoadedAnim:Play()
	LoadedAnim:GetMarkerReachedSignal(Event):Connect(function()
		player.Character.HumanoidRootPart.CFrame = partCFrame
	end)
	LoadedAnim.Stopped:Wait()
	GUI.Parent = script
end

return module

Still not getting what doesn’t work.
I could try to edit the script and tomorrow I will let out a reply. Thanks!

What I did was to change

local UnloadedAnim = AnimFolder[math.random(0,#AnimFolder)]

to

local UnloadedAnim = AnimFolder[math.random(1,#AnimFolder)]

I am stupid, aren’t I?