Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I get this first person viewmodel script to work?

Asked by 1 year ago

I'm following a tutorial for a first person viewmodel. It doesnt work and repeatedly returns the following error when I run it:

Model:SetPrimaryPartCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this. - Client - Framework - Client:38

I've already set the Primary part to the model in a replicated storage folder.

Code:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local camera = game.Workspace.CurrentCamera

local framework = {
    inventory = {
        "FNC";
        "HiPower";
        "Knife";
        "M26";
    };

    module = nil;
    viewmodel = nil;
}

function loadSlot(Item)
    local viewmodelFolder = game.ReplicatedStorage.Viewmodels
    local moduleFolder = game.ReplicatedStorage.Modules

    if moduleFolder:FindFirstChild(Item) then
        framework.module = require(moduleFolder:FindFirstChild(Item))

        if viewmodelFolder:FindFirstChild(Item) then
            framework.viewmodel = viewmodelFolder:FindFirstChild(Item):Clone()
            framework.viewmodel.Parent = camera
        end
    end
end

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

loadSlot(framework.inventory[1])

1 answer

Log in to vote
0
Answered by 1 year ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Try this

local player = game.Players.LocalPlayer
local character = player.Character or player:WaitForChild("Character")

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local camera = game.Workspace.CurrentCamera

local framework = {
    inventory = {
        "FNC";
        "HiPower";
        "Knife";
        "M26";
    };

    module = nil;
    viewmodel = nil;
}

function loadSlot(Item)
    local viewmodelFolder = game.ReplicatedStorage:WaitForChild("Viewmodels")
    local moduleFolder = game.ReplicatedStorage:WaitForChild("Modules")

    if moduleFolder:FindFirstChild(Item) then
        framework.module = require(moduleFolder:FindFirstChild(Item))

        if viewmodelFolder:FindFirstChild(Item) then
            framework.viewmodel = viewmodelFolder:FindFirstChild(Item):Clone()
            framework.viewmodel.Parent = camera
        end
    end
end

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

loadSlot(framework.inventory[1])

0
Basically "Try using WaitForChild" DindinYT37 246 — 1y
Ad

Answer this question