Problem: Animation Will not Load.
Script Bellow
Error: Unable to cast value to Object
mouse = Player:GetMouse() local SprintAnim = 389349781 local WalkAnim = 389302210 run = game:GetService("RunService") print(2) function onKeyDown(key) local Key = key:lower() if key == "r" and mode == 1 then print(1) game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 * 1.5 sound:Play() Humanoid:LoadAnimation(SprintAnim) gold.Value = true mode = 2 elseif key == "r" and mode == 2 then print(3) game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 sound:Play() Humanoid:LoadAnimation(WalkAnim) gold.Value = true mode = 1 end end
The method LoadAnimation takes an Animation object as its argument, where you are sending it an integer. This gives you the error "Unable to cast value to Object" because it attempts change (cast) the integer into the expected type (an Object) but fails to do so.
You could either store the animation objects somewhere (e.g. in ReplicatedStorage or maybe a folder in each player) or create a reference to one inside the script.
local SprintAnim = Instance.new("Animation") SprintAnim.AnimationId = "rbxassetid://389349781" local WalkAnim = Instance.new("Animation") WalkAnim.AnimationId = "rbxassetid://389302210"
You should now be able to load the animation with humanoid:LoadAnimation(SprintAnim)
and humanoid:LoadAnimation(WalkAnim)
, given that humanoid is the desired player's humanoid.
All we did now though was load the animation. It won't play because we never told it to. To tell it to play, we need to store the AnimationTrack that is returned from the LoadAnimation
method. This AnimationTrack
has methods for playing and stoping animation as well as other methods to customize how it is played.
There are a couple of other areas were I see you may run into problems at, but if you do have any future problems, ask another question and hopefully someone will assist you.