FPS Viewmodel not working

What do I want to achieve?
I’m trying to attach an FPS viewmodel to a player’s camera.

What is the issue?
My script that is meant to attach the viewmodel to the camera is not working properly, or that’s at least what I’m assuming.

What solutions have I tried so far?
I have tried rewriting my code, different viewmodels, placing my script in different places, changing my script from LocalScript to Script, and getting help from a fellow dev.

My script (this is the only script – LocalScript – in the game so far, it’s in the StarterCharacterScripts):

local camera = game.Workspace.CurrentCamera;
local humanoid = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("Humanoid");

local viewModel = game.ReplicatedStorage:WaitForChild("viewModel"):Clone();

local function onDied()
	viewModel.Parent = nil;
end

local function onUpdate(dt)
	viewModel.Head.CFrame = camera.CFrame;
end

humanoid.Died:Connect(onDied);
game:GetService("RunService").RenderStepped:Connect(onUpdate);

Setup:

Result:

Output (no errors):
image

Thanks in advance.

2 Likes

Camera updates go in StarterPlayerScripts

1 Like

Okay, I will attempt to try that, and I will reply with results.

1 Like

I switched the script to StarterPlayerScripts and it still didn’t work, I’m still not seeing the viewmodel.

1 Like
local function onUpdate(dt)
	camera.CFrame = viewModel.Head.CFrame
end

Switch the position of your variables in your method like I did above ^

1 Like

I’m getting this, now:

1 Like

Ok so the issue is that your camera is still following the default spot. What I would do is weld the viewport frame onto the players body, and then make the Camera.CFrame follow it.

1 Like

Okay, so I should weld the viewmodel arms to the players body and do this?:

local function onUpdate(dt)
	camera.CFrame = viewModel.Head.CFrame
end
1 Like

Yes something like that. I wrote some quick code to help you maybe understand it a bit better below:

local camera = game.Workspace.CurrentCamera;
local humanoid = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("Humanoid");
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local function onUpdate(dt)
    camera.CFrame = character.Head.CFrame
end

game:GetService("RunService").RenderStepped:Connect(onUpdate);

What i’m doing is just setting the cameras position to the players head. (But in your case you will need to first weld the viewModel onto the player, then do what I did above with the viewModel.Head position. It’s essentially the same thing)

2 Likes

Parent your viewmodel to the camera(or anywhere you want I guess). You are cloning it but not parenting it anywhere in the workspace so it never shows itself.

1 Like

Yeah, still doesn’t work, not sure if I’m welding it correctly

local weld = Instance.new("WeldConstraint")
weld.Part0 = character.Head
weld.Part1 = viewModel.Head
weld.Parent = character.Head
1 Like

Okay, I will attempt that and I’ll respond with the results.

1 Like

Unfortunately, it still doesn’t work.

1 Like

It’s possible you aren’t seeing your VIewmodel is due to it’s rotation. I’ve had this issue before, to essentially fix it, I’d use:

-- // This should flip the viewmodel 180 degrees. You might also need to apply it on the third component.
-- // For example, CFrame.Angles(0,math.pi,math.pi) but try the first method below :D
-- // You could alternatively use CFrame.Angles(0,math.rad(180),0) or something like that, but you'd 
-- // have to do some experimentation with that.
viewModel.Head.CFrame = camera.CFrame * CFrame.Angles(0,math.pi, 0) 

If it’s not the rotation then I wouldn’t really know :sweat_smile: just experiment with this for now and I’m sure it will eventually work :smiley:

2 Likes

I believe the problem is not with the rotation, I’m not seeing the viewmodel at all in the game. I pasted your code into my script, and it still doesn’t work, but thanks for the help :grinning:.

Is your viewmodel invisible? Also have you welded it onto the player correctly?

1 Like

Actually, you forgot to parent the viewmodel :sweat_smile:

1 Like

This isn’t a good way because when he clones the viewmodel it remains in the start position so the camera will be moved to the model and he needs the contrary. I suggest to parent the viewmodel to the player’s character and then set it’s CFrame to the player’s character HumanoidRootPart CFrame

2 Likes

Do you know where I can parent it to?

1 Like

You can parent it to either workspace.CurrentCamera or workspace. Try workspace.CurrentCamera

2 Likes