[PROBLEM] Making the ViewModel arms to follow the camera

local Tool = script.Parent
local Shirt = true
local StarterPlayer = game:GetService("StarterPlayer")
local MaxZoom = StarterPlayer.CameraMaxZoomDistance
local MinZoom = StarterPlayer.CameraMinZoomDistance
local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local SystemConfigModule = require(RS:WaitForChild("SystemConfig"))
local LocalPlayer = game:GetService("Players").LocalPlayer
local CurrentCamera = game.Workspace.CurrentCamera
local ZoomOutZ = -44.423
local equipped = false
local char = LocalPlayer.Character
local hum = char:FindFirstChild("Humanoid")
local LeftArm = char:FindFirstChild("Left Arm")
local RightArm = char:FindFirstChild("Right Arm")
local RunService = game:GetService("RunService")

local EquippedWeaponAnim = script.EquippedWeaponAnim
local animationEquippedTrack = hum:LoadAnimation(EquippedWeaponAnim)

function Arms()
	local CurrentCamera = workspace.CurrentCamera
	local Arms = Instance.new("Model")
	local existingArms = CurrentCamera:FindFirstChild("Arms")
	
	if existingArms and (LocalPlayer.CameraMaxZoomDistance == 0 and LocalPlayer.CameraMinZoomDistance == 0) then return end
	if existingArms then LocalPlayer.CameraMaxZoomDistance = 0 LocalPlayer.CameraMinZoomDistance = 0 return end
		
	Arms.Name = "Arms"
	Arms.Parent = CurrentCamera

	LocalPlayer.CameraMinZoomDistance = 0
	LocalPlayer.CameraMaxZoomDistance = 0

	if Shirt == true then
		local Humanoid = Instance.new("Humanoid")
		Humanoid.MaxHealth = 0
		Humanoid.Health = 0
		Humanoid.Name = ""
		Humanoid.Parent = Arms

		local newShirt = LocalPlayer.Character:FindFirstChild("Shirt"):clone()
		newShirt.Parent = Arms
	end

	local RightArm = LocalPlayer.Character:FindFirstChild("Right Arm"):clone()
	RightArm.Name = "Right Arm"
	RightArm.Transparency = 0
	RightArm.CanCollide = false

	local w = Instance.new("Weld")
	w.Part0 = RightArm
	w.Part1 = LocalPlayer.Character:FindFirstChild("Right Arm")
	w.C0 = CFrame.new()
	w.C1 = CFrame.new()
	w.Parent = RightArm	
	RightArm.Parent = Arms

	local LeftArm = LocalPlayer.Character:FindFirstChild("Left Arm"):clone()
	LeftArm.Name = "Left Arm"
	LeftArm.Transparency = 0	
	LeftArm.CanCollide = false
	
	local w = Instance.new("Weld")
	w.Part0 = LeftArm
	w.Part1 = LocalPlayer.Character:FindFirstChild("Left Arm")
	w.C0 = CFrame.new()
	w.C1 = CFrame.new()
	w.Parent = LeftArm	
	LeftArm.Parent = Arms
	
	Arms.PrimaryPart = RightArm
	
	LocalPlayer.CameraMaxZoomDistance = 0
	LocalPlayer.CameraMinZoomDistance = 0
end

function NonArms()
	local CurrentCamera = workspace.CurrentCamera
	if CurrentCamera:FindFirstChild("Arms") then
		CurrentCamera:FindFirstChild("Arms"):Destroy()
	end
	
	LocalPlayer.CameraMaxZoomDistance = MaxZoom
	LocalPlayer.CameraMinZoomDistance = 5
end

UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end

	local FPSValue = script.FPS

	if equipped then
		if input.KeyCode == SystemConfigModule["TPS"] then
			NonArms()
			FPSValue.Value = false
		elseif input.KeyCode == SystemConfigModule["FPS"] then
			Arms()
			FPSValue.Value = true
		end
	end
end)

Tool.Equipped:Connect(function()
	equipped = true
	
	local FPSValue = script.FPS
	
	if FPSValue.Value then
		Arms()
	else
		NonArms()
	end
	animationEquippedTrack:Play()
end)

Tool.Unequipped:Connect(function()
	equipped = false
	local CurrentCamera = game.Workspace.CurrentCamera
	for _, Model in pairs(CurrentCamera:GetChildren()) do
		if Model.Name == "Arms" then
			Model:Destroy()
		end
	end
	
	animationEquippedTrack:Stop()
	
	LocalPlayer.CameraMaxZoomDistance = MaxZoom
	LocalPlayer.CameraMinZoomDistance = MinZoom
end)

RunService.RenderStepped:Connect(function()
	for i, v in pairs(CurrentCamera:GetChildren()) do
		if v:IsA("Model") then
			v:SetPrimaryPartCFrame(CurrentCamera.CFrame)
		end
	end
end)

I wanted to create a viewModel arms for my gun system, but I ran into trouble because they wouldn’t follow the camera even if I tried setting the CFrame of its main part to the CFrame of the camera. Nevertheless, cloning the head and making it function as the primary part of that model did work (added weld), but it made the player move and fly with the camera’s movements. I’ve now added the right arm as the primarypart as it is the arm equipped with tool.

What is the best solution for this problem? I always appreciate your assistance! Thank you so much.

Script with the Head added:

	local Head = LocalPlayer.Character:FindFirstChild("Head"):Clone()
	Head.Name = "Head"
	Head.Transparency = 0
	Head.CanCollide = false
	Head.Parent = Arms
	
	local w = Instance.new("Weld")
	w.Part0 = Head
	w.Part1 = LocalPlayer.Character:FindFirstChild("Head")
	w.C0 = CFrame.new()
	w.C1 = CFrame.new()
	w.Parent = Head	
	Head.Parent = Arms
	
	LocalPlayer.CameraMaxZoomDistance = 0
	LocalPlayer.CameraMinZoomDistance = 0
-- ... function end here ...
-- ... etc ... (other codes)

RunService.RenderStepped:Connect(function()
	for i, v in pairs(CurrentCamera:GetChildren()) do
		if v:IsA("Model") then
			v:FindFirstChild("Head").CFrame = CurrentCamera.CFrame
		end
	end
end)

The following happens when I add Head: robloxapp-20240120-1438121.wmv (2.8 MB)

I also know that this is caused due to the weld, but again without that weld it doesn’t work at all. So, I just need a solution for this problem.

I think the problem is the collisions. Instead of setting the cloned body parts’ CanCollide property to false, change their CollisionGroups to a group that does not collide with anything instead. I’m not sure if this would work but whenever I make viewmodels, I always use CollisionGroups instead of CanCollide. I hope this helps!

1 Like

Thank you for the response. I will try it.

1 Like

Also, if you are going to make viewmodels, just create a pre-made viewmodel and then clone a tool that is equipped and place it in the viewmodel. Make sure to manually add a grip joint so it will be attached to the viewmodel rig. To animate the viewmodel, just copy the Transform property of every Motor6D joint in the character and set it as the Transform of the Motor6D in the viewmodel. This is what I usually do when making viewmodels so I hope this helps aswell!

1 Like

Yea, I am very new in making FPS guns. Thank you so much for the advice!

1 Like

Didn’t find any solutions yet.

Okay, I’ve solved the issue now.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.