Help with (coroutine.wrap)

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.

I think your function should be this

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

1 Like

The error is from the other line:

Edit: The function is not creating an error, the coroutine.wrap is.

I took inspiration from here. This code works perfectly fine.

Edit: The save is just a predefined function.

Edit 2: By the way, the same error is returned. @rbill13

Try out my edit

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

1 Like

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.

Eliminate the coroutine and leave the function alone to know where it fails.
@EmbatTheHybrid is right with the AnimationTrack.

2 Likes