How would I make a ViewModel that only shows in first person with a tool equipped?

Recently, I’ve found about ViewModels and how they are used in many FPS games on Roblox. I already have a gun system that I don’t want to rewrite, so I’m trying to figure out how I can have a ViewModel visible when zoomed in while having a gun equipped? Also how would I make it aim down sights as well?

5 Likes

you have to cframe it to the camera every RenderStep. Do something like this:

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local gunModel = workspace.YourGunModelName
local gunTool;

local camera = workspace.CurrentCamera
local cframeOffsetFromCamera = CFrame.new(0.5, -0.5, -1) -- change this offset to fix where you want


local function cframeToCamera()
    while gunTool and game:GetService("RunService").RenderStepped:Wait() do
        gunModel:SetPrimaryPartCFrame(camera.CFrame * cframeOffsetFromCamera)
    end
end

char.ChildAdded:Connect(function(child)
    if child:IsA("Tool") and child.Name == gunModel.Name then
        gunTool = child
        cframeToCamera()
    end
end)

char.ChildRemoved:Connect(function(child)
    if gunTool and child == gunTool then
        gunTool = nil
    end
end)
12 Likes

where should i put the script then? sorry i’m just beginner

i recommend either StarterGui or StarterCharacterScripts.

3 Likes