Hello. This is DevTenga and I have created this post as I have been facing a problem while trying to create an animation script on a few NPCs using coroutine.wrap. The code goes like this:
local function PlayAnimOnLoop(Value,FanModel)
print("GotIt")
if FanModel and FanModel.Humanoid then
local CheerAnimation = FanModel.Humanoid.Animator:LoadAnimation(FanModel.Animate.cheer.Animation.AnimationId)
FanModel.Humanoid:PlayAnimation(CheerAnimation)
FanModel.Humanoid.AnimationFinished:Connect(function() FanModel.Humanoid:PlayAnimation(CheerAnimation) end)
end
end
game.Workspace.Collection.Touched:Connect(function(Hit)
if Hit and Hit.Parent and game.Players:GetPlayerFromCharacter(Hit.Parent) then
AllowedToMove = false
for _,SideFolder in pairs(game.Workspace.Followers:FindFirstChild(Player.Name):GetChildren()) do
for _,AnimatedModel in pairs(SideFolder:GetChildren()) do
if AnimatedModel.Name == "FanModel" then
coroutine.wrap(PlayAnimOnLoop)(0,AnimatedModel)
end
end
end
end
end)
The statement: coroutine.wrap(PlayAnimOnLoop)(0,AnimatedModel) returns an error: ServerScriptService.FollowScript:93: Unable to cast value to Object
Why is this so? I’d be glad if anyone could explain it to me. Thanks for showing your interest in my problem. Your time and attention are appreciated.
EDIT: The error does not take place if I remove the AnimatedModel completely, but that defeats the purpose of the function.
local function PlayAnimOnLoop(Value,FanModel)
print("GotIt")
if FanModel and FanModel.Humanoid then
local CheerAnimation = FanModel.Humanoid.Animator:LoadAnimation(FanModel.Animate.cheer.Animation)
CheerAnimation:Play()
CheerAnimation.Looped = true
end
end
I don’t think AnimationFinished and PlayAnimation exist for the animator. From the looks of it, you just need to play the animationTrack and make it looped, not sure if you have to make it looped before or after playing it
Edit: Actually the issue is you wer etryign to use LoadAnimatino o nthe AnimationId. Try it out now @Anurag_UdayS. The error is there because it’s erroring in the function. You may have to look to see what the name of the cheer animation is
local function PlayAnimOnLoop(Value,FanModel)
print("GotIt")
if FanModel and FanModel:FindFirstChildOfClass("Humanoid") then
local CheerAnimation = FanModel.Humanoid.Animator:LoadAnimation(FanModel.Animate.cheer.Animation)
CheerAnimation:Play()
CheerAnimation.Looped = true
end
end
Your issue was that you were using LoadAnimation on the animation id of the Animation, not on the Animation Instance
Also again, it’s erroring there because it’s erroring during the function’s code
I have applied your edits. The result is actually the same. It still creates the same error. The error does not take place if I remove the AnimatedModel completely, but that defeats the purpose of the function.