Animation is failing to load

Hello! For some reason my animation is failing to load the whole time. In Studio it works fine but in a real game it doesn’t.


My Script: (Altough I don’t think it’s necessary)

wait(0.5)
local Animation = script:WaitForChild("Animation")
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild("Humanoid")
--Load the animation
local animationTrack = hum:LoadAnimation(Animation)
local mouse = player:GetMouse()
local isPlaying = false
local numDrank = 0

mouse.Button1Down:Connect(function()
	wait(0.1)
	--Check if he has an item, if yes let the animation begin
	local function findItem(char)
		for i,v in next, char:GetChildren() do
			if v:IsA("Tool") then
				return v
			end
		end
	end
	local item = findItem(char)
	if item then
		--He has an item, let the animation play
		--Check if it's playing already or not
		if isPlaying == false then
			isPlaying = true
			animationTrack:Play()
			animationTrack.Stopped:Connect(function()
				if numDrank >= 5 then
					numDrank = 0
					--Destroy it
					--Fire a Remote Event
					local ReplicatedStorage = game:GetService("ReplicatedStorage")
					local MaxTimes = ReplicatedStorage.Remotes.MaxTimes
					--Fire it
					local invoke = MaxTimes:InvokeServer(item)
			else
				isPlaying = false
				numDrank = numDrank + 1
					end
			end)
		end
		end
end)

There are 2 things that is the cause:

  1. Animation ID is invalid.
  2. Animation hasn’t loaded.

For #2, you can do ContentProvider:PreloadAsync on the animations.

2 Likes

Error: Unable to cast to array
Script:

wait(0.5)
local ContentProvider = game:GetService("ContentProvider")
local Animation = ContentProvider:PreloadAsync(script.Animation)

local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild("Humanoid")
--Load the animation
local animationTrack = hum:LoadAnimation(Animation)
local mouse = player:GetMouse()
local isPlaying = false
local numDrank = 0

mouse.Button1Down:Connect(function()
	wait(0.1)
	--Check if he has an item, if yes let the animation begin
	local function findItem(char)
		for i,v in next, char:GetChildren() do
			if v:IsA("Tool") then
				return v
			end
		end
	end
	local item = findItem(char)
	if item then
		--He has an item, let the animation play
		--Check if it's playing already or not
		if isPlaying == false then
			isPlaying = true
			animationTrack:Play()
			animationTrack.Stopped:Connect(function()
				if numDrank >= 5 then
					numDrank = 0
					--Destroy it
					--Fire a Remote Event
					local ReplicatedStorage = game:GetService("ReplicatedStorage")
					local MaxTimes = ReplicatedStorage.Remotes.MaxTimes
					--Fire it
					local invoke = MaxTimes:InvokeServer(item)
			else
				isPlaying = false
				numDrank = numDrank + 1
					end
			end)
		end
		end
end)

That is currently not how you preload it. The first argument must be an array, so you could change it to this:

local Animation = ContentProvider:PreloadAsync({script.Animation})
1 Like