Viewmodel rotation misbehaves

Happy new year everyone! i hope you are doing well, i got a script here whose viewmodel isn’t really stable, and the gun doesn’t turn in the y-axis with the viewmodel:

local tool = script.Parent
local plr = game.Players.LocalPlayer
local cam = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ViewModelTemplate = ReplicatedStorage:WaitForChild("ViewModel")
local ViewModel
local equipped = false

local function updateViewModelPosition()
    if equipped and ViewModel then
        local gunHandle = tool:FindFirstChild("Handle")
        if not gunHandle then return end

        local cameraCFrame = cam.CFrame
        local gunCFrame = gunHandle.CFrame

        local offset = CFrame.new(0, 0, -1)

        local yawRotation = CFrame.Angles(0, cameraCFrame:ToEulerAnglesYXZ(), 0)
        local pitchRotation = CFrame.Angles(cameraCFrame:ToEulerAnglesYXZ(), 0, 0)

        local viewModelCFrame = gunCFrame * offset * yawRotation * pitchRotation

        ViewModel:SetPrimaryPartCFrame(viewModelCFrame)

        for _, part in ipairs(ViewModel:GetChildren()) do
            if part:IsA("BasePart") then
                part.CanCollide = false
            end
        end
    end
end

tool.Equipped:Connect(function()
    equipped = true
    if not cam:FindFirstChild("ViewModel") then
        ViewModel = ViewModelTemplate:Clone()
        ViewModel.Parent = cam
    end
end)

tool.Unequipped:Connect(function()
    equipped = false
    if cam:FindFirstChild("ViewModel") then
        cam.ViewModel:Destroy()
    end
    ViewModel = nil
end)

RunService.RenderStepped:Connect(function()
    local humanoid = plr.Character and plr.Character:FindFirstChildOfClass("Humanoid")
    if not humanoid or humanoid.Health <= 0 then
        if cam:FindFirstChild("ViewModel") then
            cam.ViewModel:Destroy()
        end
        return
    end

    updateViewModelPosition()
end)

can someone answer??im waiting

A video showing the issue ingame would be helpful.

1 Like

alr, btw i fixed the gun turn thingy but shooting broke

robloxapp-20250112-1909226.wmv (1.7 MB)
robloxapp-20250112-1909393.wmv (36.2 KB)
robloxapp-20250112-1910585.wmv (2.3 MB)
robloxapp-20250112-1913096.wmv (2.0 MB)
robloxapp-20250112-1914388.wmv (1.3 MB)

shooting script:

local Railgun = script.Parent
local PointA = game.ReplicatedStorage.ViewModel.PointA
local players = game.Players
local plr = players.LocalPlayer
local Shot = script.Shot

local ShootEvent = game.ReplicatedStorage:WaitForChild("ShootEvent")

local function createHitscanPart(startPosition, endPosition)
	local beamPart = Instance.new("Part")
	beamPart.Anchored = true
	beamPart.CanCollide = false
	beamPart.Material = Enum.Material.Neon
	beamPart.Color = Color3.new(1, 0, 0)
	beamPart.Size = Vector3.new(0.2, 0.2, (endPosition - startPosition).Magnitude)
	beamPart.CFrame = CFrame.new(startPosition, endPosition) * CFrame.new(0, 0, -beamPart.Size.Z / 2)
	beamPart.Parent = workspace
	game.Debris:AddItem(beamPart, 0.2)
end

Railgun.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		local cursorPosition = mouse.Hit.Position
		local direction = (cursorPosition - PointA.Position).Unit
		local raycastResult = workspace:Raycast(PointA.Position, direction * 1000)
       Shot:Play()
		if raycastResult then
			createHitscanPart(PointA.Position, raycastResult.Position)
			ShootEvent:FireServer(raycastResult.Instance)
		else
			createHitscanPart(PointA.Position, PointA.Position + direction * 1000)
		end
	end)
end)

Railgun.Equipped:Connect(function()
	PointA.Parent = Railgun
end)

Railgun.Unequipped:Connect(function()
	PointA.Parent = game.ReplicatedStorage.ViewModel
end)

replic
starterpack
object locations

anything else needed? :thinking: ty for your help

What part of the shooting isn’t working? You didn’t send a clip of that.

there are no errors so i dont know