Why does this animation script not work?

So im animating my viewmodel for a gun but the shooting animation just doesnt want to work at all.

There is nothing in the output and no signs of any wrong things in the code.

The code is a serverscript bc i have a function that does something when firing the gun there.

Heres the code that i use (only the parts that actually matter on this topic)

local replicatedStorage = game.ReplicatedStorage
local VModel = replicatedStorage:WaitForChild("ViewModel")
local VModelHumanoid = VModel:WaitForChild("Humanoid")

-- Ensure the Humanoid has an Animator
local animator = VModelHumanoid:FindFirstChild("Animator")
if not animator then
	animator = Instance.new("Animator")
	animator.Parent = VModelHumanoid
end

local RecoilAnim = Instance.new("Animation")
RecoilAnim.AnimationId = "rbxassetid://73833512534529"

-- Function to play the fire sound and animation
function PlayFireSound()
	local NewSound = FireSound:Clone()
	local MuzzleFlash = script.Parent.Handle.MuzzleFlash.MuzzleEffect 
	NewSound.Parent = Handle
	NewSound:Play()
	MuzzleFlash:Emit(20)

	-- Load and play the animation
	local animationTrack = animator:LoadAnimation(RecoilAnim)
	animationTrack:Play()

	-- Clean up the sound after it finishes playing
	Debris:AddItem(NewSound, NewSound.TimeLength)
end

I have no idea whats the issue and why doesnt it work. If u know whats wrong pls reply to this topic with the answer. Thanks for helping :smiley:

1 Like

Move the loading of the animation outside the function and don’t create the animation during runtime. Create the animation before the game starts

Sry if i ask dumb questions im kind of new at scripting so i understand that i move local animationTrack = animator:LoadAnimation(RecoilAnim) to outside the function and everything before the function to the top?

1 Like

i guess im wrong with my thinking it didnt work lol

1 Like

Its probably the way you made the anim… Cz this is a gun script are you sure you did everything right?

1 Like

Not rly lol. Mayb i should make a script in the viewmodel or i heard that there are scripts for anims in starterplayerscript or startercharacterscripts. do u know if it could have worked like mayb i did a remoteevent that just passed the info when the gun shot to the starterplayerscript or startercharacterscripts or the vmodel?

1 Like

Why is your code in a server script? ViewModels should be local scripts