#View Model not working

1 messages · Page 1 of 1 (latest)

glacial sierra
#

I'm not sure where I'm wrong here, but the View Model should've appear when I scroll into first person like what happened in this video = https://www.youtube.com/watch?v=3W8C8R9X9eU

rnk

RigLite Plugin:
https://create.roblox.com/store/asset/1274343708/RigEdit-Lite?assetType=Plugin&externalSource=www

Head Movement Model:
https://create.roblox.com/store/asset/79942330840337/HeadMovement

CHAPTERS:
Intro - 00:00
Chapter 1 : Setting Up ViewModel - 00:19
Chapter 2 : Attaching ViewModel - 06:57
Chapter 3: Aiming - 18:00
Conclusion -...

▶ Play video
#
local GunName = script.Parent.Name

local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local head = character:WaitForChild("Head")
local RS = game:GetService("ReplicatedStorage")
local GunFolder = RS:FindFirstChild(GunName)
local RunService = game:GetService("RunService")

local ViewModelRemote = RS.ViewModelRemote
local ViewModelTP = GunFolder.ViewTPModel
local ViewModel
local ViewModelTPS
local equipped = false

local swayAmount = 0.6
local swayCF = CFrame.new()
local lastCameraCF = CFrame.new()

local TweenService = game:GetService("TweenService")


local function isFirstPerson()
    if head.LocalTransparencyModifier == 1 or (head.CFrame.Position - Camera.CFrame.Position).Magnitude < 1 then
        return true
    else
        return false
    end
end

local function CreateFPVModel()
    local viewModel = GunFolder.ViewModel:Clone()
    viewModel.Name = "ViewModel" .. GunName
    viewModel.Parent = Camera
end

local function CreateTPLocal()
    if not ViewModelTPS and equipped then
        ViewModelTPS = ViewModelTP:Clone()
        ViewModelTPS.Parent = player.Character
        ViewModelTPS.Name = "ViewModelTPS(Local)" .. GunName
    end
end

local function destroyFPViewModel_createTPSViewModel()
    if Camera:FindFirstChild("ViewModel" .. GunName) then
        Camera:FindFirstChild("ViewModel" .. GunName):Destroy()
    end
    
    CreateTPLocal()
end




tool.Equipped:Connect(function()
    equipped = true
    end)

tool.Unequipped:Connect(function()
    equipped = false
    
    if Camera:FindFirstChild("ViewModel" .. GunName) then
        Camera:FindFirstChild("ViewModel" .. GunName):Destroy()
    end
    if player.Character:FindFirstChild("ViewModelTPS(Local)" .. GunName) then
        ViewModelTPS = nil
        player.Character["ViewModelTPS(Local)" .. GunName]:Destroy()
    end
end)
#
RunService.RenderStepped:Connect(function()
    if player.Character.Humanoid.Health <= 0 then
        equipped = false
        return
    end
    
    if not equipped then return end
    
    if isFirstPerson() then
        
        local FPViewModel = Camera:FindFirstChild("ViewModel" .. GunName)
        local TPSViewModel = player.Character:FindFirstChild("ViewModelTPS(Local)" .. GunName)
    
        if not FPViewModel then 
            CreateFPVModel()
        else
            if TPSViewModel then
                ViewModelTPS = nil
                TPSViewModel:Destroy()
            end
            
            local rot = Camera.CFrame:ToObjectSpace(lastCameraCF)
            local X, Y = rot:ToOrientation()
            swayCF = swayCF:Lerp(CFrame.Angles(math.sin(X) * swayAmount, math.sin(Y) * swayAmount, 0), 0.1)
            lastCameraCF = Camera.CFrame
            
            local primaryPart = FPViewModel.PrimaryPart
            
            FPViewModel:SetPrimaryPartCFrame(Camera.CFrame * swayCF)
        end
    else
        destroyFPViewModel_createTPSViewModel()
        if player.Character and ViewModelTPS then
                ViewModelTPS:PivoteTo(player.Character.Head.CFrame)
        end
    end
end)