Simple Viewmodel!

Well, welcome back to another devforum created by me. Today, we will be creating a simple viewmodel with custom offsets using ModuleScripts, RunService, and Motor6D. Let’s get started.


Intro and Setting Up
First of we create a HumanoidRootPart, a LeftArm, and a RightArm. Then, in the HumanoidRootPart we add 2 Motor6D’s that are attached to the Arms.

Sources

Screenshot 2022-08-03 183948
Screenshot 2022-08-03 184001
https://gyazo.com/a72552cc76bad69589a59d84c3aa88a9
ViewmodelOpenSource.rbxl (40.4 KB)


Scripting Part
We add a LocalScript into StarterPlayerScripts named “ViewmodelClient” and a ModuleScript into ReplicatedStorage named “Offsets”.

In the ModuleScript we add the name of the viewmodel that’s equal to a cframe offset.

return {
	["Viewmodel"] = CFrame.new(0, 0, 0);
}

Next, in the LocalScript we first off add the services.

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

Then, we require the offsets module, get the camera, cloned the Viewmodel, and set the Viewmodel’s parent equal to the Camera.

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

local Offsets = require(ReplicatedStorage:WaitForChild("Offsets"))
local Camera = workspace.CurrentCamera
local ClonedModel = game:GetService("ReplicatedStorage"):WaitForChild("Viewmodel"):Clone()

ClonedModel.Parent = Camera

After, we get the Camera, ModuleScript, and the model that we are cloning, we run the RenderStepped Function with an if statement checking if the model is not equal to nil and seeing if the model name is in the offsets module and also check if the viewmodel has a HumanoidRootPart to set it’s cframe.

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

local Offsets = require(ReplicatedStorage:WaitForChild("Offsets"))
local Camera = workspace.CurrentCamera
local ClonedModel = game:GetService("ReplicatedStorage"):WaitForChild("Viewmodel"):Clone()

ClonedModel.Parent = Camera

RunService.RenderStepped:Connect(function()
	if ClonedModel ~= nil and Offsets[ClonedModel.Name] then
		if ClonedModel:FindFirstChild("HumanoidRootPart") ~= nil then
            ClonedModel.HumanoidRootPart.CanCollide = false
			ClonedModel["Left Arm"].CanCollide = false
			ClonedModel["Right Arm"].CanCollide = false
			ClonedModel.HumanoidRootPart.CFrame = Camera.CFrame * Offsets[ClonedModel.Name]
		end
	end
end)

EDIT: To turn off the shadows turn off CastShadows property in the explorer tab

13 Likes

Oh, Im the first one to comment? Nice

Your tutorial is so awesome!

2 Likes

The current edit to the post was to remove the collision to the view model!

2 Likes