Struggling to Load Animation For ViewModel

So I made a ViewModel in my tool, and I’m trying to load the animation, and clone the ViewModel. But it not working. The Error is:

ViewModelAnimation is not a valid member of Tool “Players.papahetfan.Backpack.AR15” - Client - LocalScript:3

Here is my Script:

local RATE_PER_SECOND = 0
local RunService = game:GetService("RunService")
local PlayAnimation = script.Parent.AnimationController.Animator:LoadAnimation(script.Parent.Parent.ViewModelAnimation.Hold)

script.Parent.Parent.Equipped:Connect(function()
	script.Parent.Parent.Handle.Transparency = 1
	PlayAnimation:Play()
	
	RunService.RenderStepped:Connect(function(step)
		local increment = RATE_PER_SECOND * step
		script.Parent.PrimaryPart.CFrame = game.Workspace.CurrentCamera.CFrame
	end)
end)

And here is a screenshot
Screen Shot 2022-07-04 at 2.56.35 PM

Help Soon,

papahetfan

Looks like you have one too many Parents in the specification of where your ViewModelAnimation is. Take one of them out and it should work… If the script is Server or Client scripts.

The script is in the ViewModel, and it is a Local Script

Here is what is inside the Model:
Screen Shot 2022-07-05 at 1.02.59 PM

If you add print(script.Parent.Parent) right before the line with the error, what does that print out?

EDIT: I just noticed that your error says it’s in the Backpack. Is the tool equipped?

it prints “AR15 - Client - LocalScript:6”

That looks right. I just noticed that your error has Backpack as the parent of the tool. Is the tool equipped?

Try this:

local viewModelAnimation = script.Parent.Parent:FindFirstChild("ViewModelAnimation")
script.Parent.AnimationController.Animator:LoadAnimation(viewModelAnimation.Hold)

If that doesn’t work, try this next:

local viewModelAnimation = script.Parent.Parent:WaitForChild("ViewModelAnimation")
script.Parent.AnimationController.Animator:LoadAnimation(viewModelAnimation.Hold)

EDIT: I left Hold off the end of viewModelAnimation. Just caught it.

1 Like

The :WaitForChild() Works, thank you!

No problem. The reason that you were having problems is that this is a known race condition. Even though things ‘should’ be loaded, it takes time for them to load. So if a script is asking for it before it’s ready, you will get all sorts of errors.

Please mark my post as the solution. Thanks.

Will do! For this script, I thought :WaitForChild() would ruin the gun scripts, but guess not.

1 Like