I need help with my viewmodel script, the view model is not infront of me

  1. What do you want to achieve? Keep it simple and clear!

so I’m making a view model script.

  1. What is the issue? Include screenshots / videos if possible!

it doesn’t work like the view model is not infront of me…
image

it also gave me this error in the output.

Players.TheUndeadPear.PlayerGui.ClientScripts.LocalScript:31: invalid argument #2 (Vector3 expected, got nil) viewModel:SetPrimaryPartCFrame(camera.CFrame*cf)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried looking for solutions on devforum, and google search it but I couldn’t seem to find the way to fix my script.

I’d be really appreciated if someone could help me with this.

--// services
local runService = game:GetService('RunService')
local replicatedStorage = game:GetService('ReplicatedStorage')
local players = game:GetService('Players')
--// objects
local viewModel = replicatedStorage:WaitForChild('MainGame'):WaitForChild('Guns'):WaitForChild('ViewModel'):Clone()
local camera = workspace.CurrentCamera
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild('Humanoid')
--// modules
local config = require(viewModel:WaitForChild('Config'))
--// Main Code
local cf = CFrame.new()

viewModel.Parent = camera

local walkAnimationSpeed = 5

local walkAnimationSize = 4

local animations = {
    ['Hold'] = viewModel.AnimationController.Animator:LoadAnimation(viewModel.Animations.Hold)
}

animations['Hold']:Play()

cf = config.OffSet_From_Camera

function positionModel()
    viewModel:SetPrimaryPartCFrame(camera.CFrame*cf)
    if humanoid.MoveDirection.Magnitude > 0 then
        cf = cf:Lerp(CFrame.new(config.Offset_From_Camera.X+math.sin(time()*walkAnimationSpeed)/walkAnimationSize, config.Offset_From_Camera.Y+math.cos(time()*walkAnimationSpeed)/walkAnimationSize,config.Offset_From_Camera.Z),.2)
    else
        cf = cf:Lerp(config.Offset_From_Camera,0.2)
    end
end

runService.RenderStepped:Connect(positionModel)

Did you set a CFrame offset from camera? I believe this variable is nil.

Also the script seems similar to this with the same error though it might be a coincidence.

Check this tutorial out:

I did in module script. And yes it was a coincidence. I did not know someone has a similar script as mine xd