Unable to cast value to object for an animation

Hey I am making a gun with animations and it is saying Unable to cast value to Object for the parts with the :LoadAnimation

-- Server script in the tool

local Tool = script.Parent
local OnShootRE = Tool:WaitForChild("OnShoot")
local HolsterRE = Tool:WaitForChild("Holster")
local ReloadRE = Tool:WaitForChild("Reload")
local ShootOrigin = Tool:WaitForChild("ShootOrigin")

local HoldingAnim = script:WaitForChild("HoldingAnim")
local HolsteredAnim = script:WaitForChild("HolsteredAnim")
local ReloadingAnim = script:WaitForChild("ReloadingAnim")

local ServerStorage = game:GetService("ServerStorage")

OnShootRE.OnServerEvent:Connect(function(Player, Position)
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {script.Parent.Parent}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	
	local Origin = ShootOrigin.Position
	local Direction = (Position - Origin).Unit
	local Result = workspace:Raycast(ShootOrigin.Position, Direction * 300, Params)
	
	local Intersection = Result and Result.Position or Origin + Direction * 300
	local Distance = (Origin - Intersection).Magnitude
	
	local BulletClone = ServerStorage.Bullet:Clone()
	BulletClone.Size = Vector3.new(0.1, 0.1, Distance)
	BulletClone.CFrame = CFrame.new(Origin, Intersection) * CFrame.new(0, 0, -Distance / 2)
	BulletClone.Parent = workspace
	
	if Result then
		local Part = Result.Instance
		local Humanoid = Part.Parent:FindFirstChild("Humanoid") or Part.Parent.Parent:FindFirstChild("Humanoid")
		
		if Humanoid then
			Humanoid:TakeDamage(20)
		end
	end
	
	wait(0.25)
	BulletClone:Destroy()
end)

ReloadRE.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")
	
	local LoadedReload = Humanoid:LoadAnimation(ReloadingAnim.AnimationId)
	
	LoadedReload:Play()
end)

HolsterRE.OnServerEvent:Connect(function(Player, Holster)
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")
	
	local LoadedHolding = Humanoid:LoadAnimation(HoldingAnim.AnimationId)
	local LoadedHolstered = Humanoid:LoadAnimation(HolsteredAnim.AnimationId)
	
	if Holster then
		LoadedHolstered:Play()
	else
		LoadedHolding:Play()
	end
end)

On what line and script is the error occuring?

Lines 47, 56 and 57. (Limit of characters)

LoadAnimation expects an animation instance, remove the AnimationId portion because it returns a string

4 Likes

This is because you are attempting to load the AnimationId instead of the Animation instance itself.
Line 47 should look as follows:

local LoadedReload = Humanoid:LoadAnimation(ReloadingAnim)

You should apply the same logic for the rest of the errors.

Also, I’d suggest loading the animation from the Animator, Humanoid:LoadAnimation() is deprecated although it will still work.