Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Trying to load animation gives me error, Unable to cast value to object?

when i execute this i get the error Unable to cast value to object

function click()
	local animation = 2652684706
local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid
local animationTrack = humanoid:LoadAnimation(animation)
animationTrack:Play()
end
script.Parent.MouseButton1Click:Connect(click)

2 comments

1

Animation should be an animation object not the ID

0

what he said

2 answers

If you're not using an Animation Object, then index it through URL, Eg.

local animation = "https://www.roblox.com/Asset?ID=animationid"

1 comment

0

tell me if that helps you

Humanoids LoadAnimation method requires you to pass an instance type of "Animation"

Example code you can try building off of:

local players = game:GetService("Players")
local client = players.LocalPlayer

local character = client.Character or client.CharacterAdded:Wait()
local humanoid = character.Humanoid

local animationId = 1234567890

local animation = Instance.new("Animation")
animation.AnimationId = ("rbxassetid://%s"):format(animationId)

local track = humanoid:LoadAnimation(animation)

track:Play()

Lessons to learn: You want to pass an instance of Animation through LoadAnimation The wiki usually has some documentation to help you (https://developer.roblox.com/api-reference/function/Humanoid/LoadAnimation)

This discussion is preserved as it was. New answers, comments, and votes are closed.