"Unable to cast value to Object" error when playing animation

Hello guys, so i got a this error with the name of “Unable to cast value to Object” when playing a animation for my gun, i dont know what is the problem, i dont know if the variable is wrong or something related, so i post here so you guys can help me, thanks in advance!

[18:05:02.761 - Unable to cast value to Object
18:05:02.763 - Stack Begin
[18:05:02.763 - Script ‘Players.lucasprofesional123.Backpack.glock.GunLocal’, Line 32]
18:05:02.764 - Stack End

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local CanFire = true
local reloading = false
local tool = script.Parent
local origin = tool:WaitForChild("Glock").Origin
local contextactionservice = game:GetService("ContextActionService")
local fireEvent = game.ReplicatedStorage.GunRemotes.GunFire
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChild("Humanoid")
local animation = Instance.new("Animation")
local HitmarkerIcon = "http://www.roblox.com/asset?id=316279305"
local shootAnim = animation.AnimationId == "rbxassetid://5427964917"
local animationn = animation.AnimationId == "rbxassetid://5419954145"
local reloadingAnim = animation.AnimationId == "rbxassetid://5427237386"
local CurrentAmmo = script.Parent.Ammo
local Magazines = script.Parent.Magazines
local GlockGUI = script.Parent.GlockGUI.hey
local ReloadingEvent = game.ReplicatedStorage.GunRemotes.Gunreload
local ProjectilesFolder = workspace.GunProjectiles
-- SCRIPTING 

mouse.TargetFilter = ProjectilesFolder




tool.Equipped:Connect(function()
	GlockGUI.Visible = true
	mouse.Icon = HitmarkerIcon -- Changing mouse icon to a cool crosshair
	game.ReplicatedStorage.Connect6D:FireServer(tool.Glock.BodyAttach)
	local equipAnimation = humanoid:LoadAnimation(animationn):Play()
	
	
	equipAnimation:GetMarkerReachedSingal("canFire"):Connect(function(fire,mouse)
		mouse.Button1Down:Connect(function() --Shoot event 
			if not reloading and CanFire then
				
				local originRay = origin.Position
				local mouseRay = mouse.Hit.Position
				fireEvent:FireServer(originRay, mouseRay)
				CurrentAmmo = CurrentAmmo - 1
				GlockGUI.Text = CurrentAmmo .. "|" .. Magazines
				
				
			end
				wait(0.3)
				CanFire = false 
				humanoid:LoadAnimation(shootAnim):Play()
			
			end)
		end)
	
-- Headstackk animation method
	char.Torso.ToolGrip.Part0 = char.Torso
	char.Torso.ToolGrip.Part1 = tool.Glock.BodyAttach
	
	
	
	
end)


tool.Unequipped:Connect(function()
	GlockGUI.Visible = false
	game.ReplicatedStorage.Disconnect6D:FireServer()
	
	
	
	
end)


2 Likes

You have to use instance.new for example
tool.Equipped:Connect(function()
GlockGUI.Visible = true
mouse.Icon = HitmarkerIcon – Changing mouse icon to a cool crosshair
local animation = Instance.new(“Animation”)
animation.AnimationId = animationn
local equipAnimation = humanoid:LoadAnimation(animation):Play()