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);
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.
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)
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.
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 just experiment with this for now and I’m sure it will eventually work
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 .
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