Motor6D Gun Issue

Context

Hi. I just started developing a FPS game when I encountered an issue. For some reason when I test play my game the gun does not seem to be positioned correctly, but when I set up the AK47 before testing without the usage of scripts, its fine. This is a problem because eventually I want to add the ability to equip different weapons.

Script (Parented to viewmodel):

local viewmodel = workspace.Viewmodel
local gun = game.ReplicatedStorage.AK47
gun.Parent = viewmodel

local motor6d = Instance.new("Motor6D")
motor6d.Parent = viewmodel.HumanoidRootPart

motor6d.Name = "Main"

motor6d.Part0 = motor6d.Parent
motor6d.Part1 = gun.Main

viewmodel.Humanoid.Animator:LoadAnimation(viewmodel.Animation):Play() 

Playtesting:

image

Blender:

image