Hello, These are of my first times experimenting with viewmodels, and i was wondering, how could i make so all tools with Handle (generic tools) , Attach to the vm arm?, right now the current behavior is this one
It just follows the position as if you were in first person, ignoring the vm, now, the same happens with tools that use motor6d, but right now i am getting it to work with handles first
This is the Viewmodel LocalScript that i am using :
task.wait()
local RunService = game:GetService("RunService")
local PhysicsService = game:GetService("PhysicsService")
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local Workspace = game:GetService("Workspace")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Camera = Workspace.CurrentCamera
local GeneralSettings = {
CanSwing = true;
CanBobble = true;
CanRecoil = true;
RecoilYRotMax = 16;
RecoilZPosMax = 1;
RecoilYPosMax = 0.5;
HeadOffset = 0;
ArmsVisible = true;
ArmTransparency = 0;
CameraAnimation = false; -- Must have "FakeCamera" and its joint with same name in order to enable this
CustomGripNames = {
["ToolGrip"] = true,
}
}
if Camera:FindFirstChild("Viewmodel") then
Camera:FindFirstChild("Viewmodel"):Destroy()
end
if Camera:FindFirstChild("ViewmodelBox") then
Camera:FindFirstChild("ViewmodelBox"):Destroy()
end
local Miscs = ReplicatedStorage:WaitForChild("Miscs")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local ViewmodelRigs = Miscs.ViewmodelRigs
local Utilities = require(Modules.Utilities)
local Math = Utilities.Math
local Spring = Utilities.Spring
local Character = script.Parent
local Head = Character:WaitForChild("Head")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
-- add near your other locals
local MotorRigToolClone = nil
local MotorRigMotorMap = {} -- [realMotor] = fakeMotor
local MotorRigPartMap = {} -- [realToolPart] = fakeToolPart
local CharacterToViewmodelPart = {}
local ViewmodelTool = nil
local ViewmodelRightGrip = nil
local OffStates = {"Jumping", "PlatformStanding", "Ragdoll", "Seated", "FallingDown", "FreeFalling", "GettingUp", "Swimming", "Climbing"}
local OnStates = {"Running"}
local ArmParts = {}
local RigType = nil
local IsAiming = false
local ShowViewmodel = false
local CurrentArmTransparency = GeneralSettings.ArmTransparency
local OriginalAimSpeed = 15
local speed = 0
local distance = 0
local velocity = Vector3.new()
local c1 = 0
local c2 = 0
local c3 = 0
local c4 = 0
local c5 = 0
local c6 = 0
local c7 = CFrame.new()
--Legends:
--[s]: Speed
--[d]: Damper
--[t]: Target
--[p]: Position
--[v]: Velocity
local SpeedSpring = Spring.spring.new()
SpeedSpring.s = 16
local SwaySpring = Spring.spring.new(0)
SwaySpring.s = 8
SwaySpring.d = 1
local SwaySpeed = Spring.spring.new(0)
SwaySpeed.s = 1
SwaySpeed.d = 1
SwaySpeed.t = 1
local VelocitySpring = Spring.spring.new(Vector3.new())
VelocitySpring.s = 16
VelocitySpring.t = Vector3.new()
VelocitySpring.p = Vector3.new()
local AimSpring = Spring.spring.new()
AimSpring.s = OriginalAimSpeed
AimSpring.d = 0.9
AimSpring.t = 0
local SwingSpring = Spring.spring.new(Vector3.new())
SwingSpring.s = 10
SwingSpring.d = 0.75
--R15
local LowerTorso
local UpperTorso
local Root
local Waist
local VMLowerTorso
local VMUpperTorso
local VMRoot
local VMWaist
local LeftUpperArm
local LeftLowerArm
local LeftHand
local RightUpperArm
local RightLowerArm
local RightHand
local LeftElbow
local LeftWrist
local RightElbow
local RightWrist
local VMLeftUpperArm
local VMLeftLowerArm
local VMLeftHand
local VMRightUpperArm
local VMRightLowerArm
local VMRightHand
local VMLeftElbow
local VMLeftWrist
local VMRightElbow
local VMRightWrist
--R6
local ShirtTexture
local Torso
local RootHip
local VMTorso
local VMRootHip
local LeftArm
local RightArm
local VMLeftArm
local VMRightArm
--General
local VMHumanoid
local VMHead
local VMHumanoidRootPart
local LeftShoulder
local RightShoulder
local VMLeftShoulder
local VMRightShoulder
local FakeCamera
local FakeCameraJoint
local OldCamCF
local CustomGrip
local LeftGrip
local RightGrip
local LastPart0C
local LastPart0L
local LastPart0R
local CurrentTool
local Setting
local Offset
local DescendantAddedConnection
local Viewmodel = ViewmodelRigs[Humanoid.RigType.Name]:Clone()
Viewmodel.Name = "Viewmodel"
local ViewmodelBox = Miscs.ViewmodelBox:Clone()
ViewmodelBox.Parent = Camera
VMHumanoid = Viewmodel:WaitForChild("Humanoid")
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.Flying, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.Freefall, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.Landed, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.Physics, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.PlatformStanding, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.Running, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.RunningNoPhysics, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.Seated, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.Swimming, false)
VMHumanoid:SetStateEnabled(Enum.HumanoidStateType.StrafingNoPhysics, false)
Viewmodel.PrimaryPart.CFrame = CFrame.new(0, 10e5, 0)
Viewmodel.Parent = Camera
local successful, errorMessage = pcall(function()
VMHumanoid:ApplyDescription(Players:GetHumanoidDescriptionFromUserIdAsync(Player.UserId))
end)
for _, Part in pairs(Viewmodel:GetDescendants()) do
if Part:IsA("BasePart") then
Part.CollisionGroup = "Viewmodel"
local LowerName = Part.Name:lower()
if LowerName:match("arm") or LowerName:match("hand") then
table.insert(ArmParts, Part)
end
elseif Part:IsA("Decal") then
Part:Destroy()
elseif Part:IsA("Accessory") then
Part:Destroy()
elseif Part:IsA("BaseScript") then
Part:Destroy()
elseif Part:IsA("ForceField") then
Part:Destroy()
end
end
VMHead = Viewmodel:WaitForChild("Head")
VMHumanoidRootPart = Viewmodel:WaitForChild("HumanoidRootPart")
if Humanoid.RigType == Enum.HumanoidRigType.R15 then
RigType = "R15"
-- setup variables
LowerTorso = Character:WaitForChild("LowerTorso")
UpperTorso = Character:WaitForChild("UpperTorso")
Root = LowerTorso:WaitForChild("Root")
Waist = UpperTorso:WaitForChild("Waist")
LeftUpperArm = Character:WaitForChild("LeftUpperArm")
LeftLowerArm = Character:WaitForChild("LeftLowerArm")
LeftHand = Character:WaitForChild("LeftHand")
RightUpperArm = Character:WaitForChild("RightUpperArm")
RightLowerArm = Character:WaitForChild("RightLowerArm")
RightHand = Character:WaitForChild("RightHand")
LeftShoulder = LeftUpperArm:WaitForChild("LeftShoulder")
RightShoulder = RightUpperArm:WaitForChild("RightShoulder")
LeftElbow = LeftLowerArm:WaitForChild("LeftElbow")
LeftWrist = LeftHand:WaitForChild("LeftWrist")
RightElbow = RightLowerArm:WaitForChild("RightElbow")
RightWrist = RightHand:WaitForChild("RightWrist")
--
VMLowerTorso = Viewmodel:WaitForChild("LowerTorso")
VMUpperTorso = Viewmodel:WaitForChild("UpperTorso")
VMRoot = VMLowerTorso:WaitForChild("Root")
VMWaist = VMUpperTorso:WaitForChild("Waist")
VMLeftUpperArm = Viewmodel:WaitForChild("LeftUpperArm")
VMLeftLowerArm = Viewmodel:WaitForChild("LeftLowerArm")
VMLeftHand = Viewmodel:WaitForChild("LeftHand")
VMRightUpperArm = Viewmodel:WaitForChild("RightUpperArm")
VMRightLowerArm = Viewmodel:WaitForChild("RightLowerArm")
VMRightHand = Viewmodel:WaitForChild("RightHand")
VMLeftShoulder = VMLeftUpperArm:WaitForChild("LeftShoulder")
VMRightShoulder = VMRightUpperArm:WaitForChild("RightShoulder")
VMLeftElbow = VMLeftLowerArm:WaitForChild("LeftElbow")
VMLeftWrist = VMLeftHand:WaitForChild("LeftWrist")
VMRightElbow = VMRightLowerArm:WaitForChild("RightElbow")
VMRightWrist = VMRightHand:WaitForChild("RightWrist")
else
RigType = "R6"
-- setup variables
Torso = Character:WaitForChild("Torso")
LeftArm = Character:WaitForChild("Left Arm")
RightArm = Character:WaitForChild("Right Arm")
RootHip = HumanoidRootPart:FindFirstChild("Root Hip") or HumanoidRootPart:FindFirstChild("RootJoint")
LeftShoulder = Torso:WaitForChild("Left Shoulder")
RightShoulder = Torso:WaitForChild("Right Shoulder")
--
VMTorso = Viewmodel:WaitForChild("Torso")
VMLeftArm = Viewmodel:WaitForChild("Left Arm")
VMRightArm = Viewmodel:WaitForChild("Right Arm")
VMRootHip = VMHumanoidRootPart:FindFirstChild("Root Hip") or VMHumanoidRootPart:FindFirstChild("RootJoint")
VMLeftShoulder = VMTorso:WaitForChild("Left Shoulder")
VMRightShoulder = VMTorso:WaitForChild("Right Shoulder")
end
if GeneralSettings.CameraAnimation then
FakeCamera = Character:WaitForChild("FakeCamera")
FakeCameraJoint = HumanoidRootPart:WaitForChild("FakeCamera")
end
local MainHolder = ViewmodelBox.MainHolder
MainHolder.Part0 = ViewmodelBox
MainHolder.Part1 = VMHumanoidRootPart
local function ViewmodelBob(a, r, basewalkspeed)
local a, r = a or 1, r or 1
local d, s, v = distance * 6.28318 * 3 / 4, speed, -velocity
local w = Vector3.new(r * math.sin(d / 4 - 1) / 256 + r * (math.sin(d / 64) - r * v.z / 4) / 512, r * math.cos(d / 128) / 128 - r * math.cos(d / 8) / 256, r * math.sin(d / 8) / 128 + r * v.x / 1024) * s / 20 * 6.28318
return CFrame.new(r * math.cos(d / 8 - 1) * s / 196, 1.25 * a * math.sin(d / 4) * s / 512, 0) * Math.FromAxisAngle(w)
end
local function ViewmodelSway(a)
local d, s = os.clock() * 6, 2 * (1.2 - a)
return CFrame.new(math.cos(d / 8) * s / 128, -math.sin(d / 4) * s / 128, math.sin(d / 16) * s / 64)
end
local function CheckIfAlive()
return (Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid:GetState() ~= Enum.HumanoidStateType.Dead)
end
local function IsFirstPerson()
return (Camera.Focus.Position - Camera.CoordinateFrame.Position).Magnitude <= 1
end
local function GetTool()
return Character:FindFirstChildOfClass("Tool")
end
local function VisibleArms(bool)
if GeneralSettings.ArmsVisible then
local castshadow = not bool
for i, part in ipairs(ArmParts) do
part.LocalTransparencyModifier = CurrentArmTransparency
part.CastShadow = castshadow
end
end
end
local function SetViewmodelVisible(bool, hasScopeEnabled)
CurrentArmTransparency = hasScopeEnabled and 1 or (bool == false and 0 or GeneralSettings.ArmTransparency)
VisibleArms(bool)
end
local function GetViewmodelPartFromCharacterPart(part)
if not part then
return nil
end
return CharacterToViewmodelPart[part]
end
-- OLD TOOLS
local function GetVMRightHand()
if RigType == "R15" then
return VMRightHand
else
return VMRightArm
end
end
local function ClearViewmodelTool()
end
local function SetupViewmodelTool(tool)
if not tool or not tool:IsA("Tool") then return end
if not tool.RequiresHandle then return end
ClearViewmodelTool()
--should be here
end
local function OnChildAdded(obj)
if obj:IsA("Tool") then
task.wait()
CurrentTool = obj
--SetupViewmodelTool(obj)
end
end
local function OnChildRemoved(obj)
if obj:IsA("Tool") then
CurrentTool = nil
--ClearViewmodelTool()
end
end
local function Disable()
RunService:UnbindFromRenderStep("UpdateViewmodel")
Viewmodel:Destroy()
ViewmodelBox:Destroy()
end
-- update character's off states
for _, state in pairs(OffStates) do
Humanoid[state]:Connect(function()
Active = false
end)
end
-- update character's on states
for _, state in pairs(OnStates) do
Humanoid[state]:Connect(function(speed)
Active = (speed > 1)
end)
end
function _G.SetAimEnabled(Value, Id)
IsAiming = Value
end
function _G.RecoilViewmodel(Value)
c1 = Value
end
function _G.SetViewmodelTransparent(Enabled)
local LocalTransparency = Enabled and 1 or 0
if CurrentTool then
for _, child in pairs(CurrentTool:GetDescendants()) do
if child:IsA("Beam") or child:IsA("Trail") or child:IsA("ParticleEmitter") then
if not (child.Name == "SmokeTrail") then
child.Enabled = not Enabled
end
end
if child:IsA("RodConstraint") or child:IsA("RopeConstraint") or child:IsA("SpringConstraint") then
child.Visible = not Enabled
end
if child:IsA("BasePart") or child:IsA("Texture") or child:IsA("Decal") then
child.LocalTransparencyModifier = LocalTransparency
end
end
end
SetViewmodelVisible(ShowViewmodel, Enabled)
end
for i, v in pairs(Character:GetChildren()) do
OnChildAdded(v)
end
Character.ChildAdded:Connect(OnChildAdded)
Character.ChildRemoved:Connect(OnChildRemoved)
RunService:BindToRenderStep("UpdateViewmodel", Enum.RenderPriority.Camera.Value + 1, function(dt)
local MouseDelta = UserInputService:GetMouseDelta()
c1 = c1 > 0.1 and Math.Lerp(c1, 0, dt * 11) or 0
local a = VelocitySpring.v
SwingSpring.t = Vector3.new(a.z / 1024 / 32 + a.y / 1024 / 16 + (MouseDelta.y / 10) / 1024 * 3 / 2, a.x / 1024 / 32 + (MouseDelta.x / 10) / 1024 * 3 / 2, -(MouseDelta.x / 10) / 1024 * 3 / 2)
local RelativeVector = CFrame.new().VectorToObjectSpace(HumanoidRootPart.CFrame, HumanoidRootPart.Velocity)
SpeedSpring.t = (Active and Humanoid.Health > 0) and (Vector3.new(1, 0, 1) * RelativeVector).magnitude or 0
VelocitySpring.t = RelativeVector
speed = SpeedSpring.p
distance = distance + dt * SpeedSpring.p
velocity = VelocitySpring.p
if CheckIfAlive() and IsFirstPerson() then
if GeneralSettings.CameraAnimation then
local NewCamCF = FakeCameraJoint.Transform
if OldCamCF then
local _, _, Z = NewCamCF:ToOrientation()
local X, Y, _ = NewCamCF:ToObjectSpace(OldCamCF):ToEulerAnglesXYZ()
Camera.CFrame = Camera.CFrame * CFrame.Angles(X, Y, -Z)
end
OldCamCF = NewCamCF
end
c2 = Math.Lerp(c2, GeneralSettings.RecoilYRotMax * c1, c1 == 0 and dt * 7 or dt * 6) + Math.Lerp(c3, Math.Randomize(2) * c1, c1 == 0 and dt * 7 or dt / 2)
c3 = Math.Lerp(c3, Math.Randomize(1.3) * c1, c1 == 0 and dt * 7 or dt * 3)
c4 = Math.Lerp(c4, Math.Randomize(5) * c1, c1 == 0 and dt * 7 or dt / 3)
c5 = Math.Lerp(c5, GeneralSettings.RecoilYPosMax * c1, c1 == 0 and dt * 7 or dt * 0.4)
c6 = Math.Lerp(c6, GeneralSettings.RecoilZPosMax * c1, c1 == 0 and dt * 7 or dt * 0.9)
c7 = CFrame.new(0, c5, c6) * CFrame.Angles(math.rad(c2), c3, c4)
local CF = CFrame.new()
CF = Camera.CoordinateFrame * CFrame.new(0, 0, (Camera.FieldOfView / 90 - 1) * 0.3)
if GeneralSettings.CanSwing then
CF = CF * Math.FromAxisAngle(SwingSpring.v)
end
if GeneralSettings.CanBobble then
CF = CF * ViewmodelBob(0.7 - 0.3 * AimSpring.p, 1 - 0.8 * AimSpring.p, Humanoid.WalkSpeed)
end
if GeneralSettings.CanRecoil then
CF = CF * c7
end
local CamOffset = -Humanoid.CameraOffset
ViewmodelBox.CFrame = (CF * CFrame.new(CamOffset.X * 0.5, CamOffset.Y * 0.5, CamOffset.Z * 0.5)) + (Camera.CoordinateFrame.UpVector * (-1.5 - GeneralSettings.HeadOffset))
--[[
if CustomGrip and LastPart0C then
CustomGrip.Part0 = Viewmodel:FindFirstChild(LastPart0C.Name, true)
end
if LeftGrip and LastPart0L then
LeftGrip.Part0 = Viewmodel:FindFirstChild(LastPart0L.Name, true)
end
if RightGrip and LastPart0R then
RightGrip.Part0 = Viewmodel:FindFirstChild(LastPart0R.Name, true)
end
--]]
--
if not ShowViewmodel then
ShowViewmodel = true
SetViewmodelVisible(ShowViewmodel)
end
else
ViewmodelBox.CFrame = CFrame.new(0, 10e5, 0)
--if CustomGrip and LastPart0C then
-- CustomGrip.Part0 = LastPart0C
--end
--[[
if LeftGrip and LastPart0L then
LeftGrip.Part0 = LastPart0L
end
if RightGrip and LastPart0R then
RightGrip.Part0 = LastPart0R
end
--]]
if ShowViewmodel then
ShowViewmodel = false
SetViewmodelVisible(ShowViewmodel)
end
end
if RigType == "R15" then
VMRoot.Transform = Root.Transform
VMWaist.Transform = Waist.Transform
VMLeftShoulder.Transform = LeftShoulder.Transform
VMRightShoulder.Transform = RightShoulder.Transform
VMLeftElbow.Transform = LeftElbow.Transform
VMLeftWrist.Transform = LeftWrist.Transform
VMRightElbow.Transform = RightElbow.Transform
VMRightWrist.Transform = RightWrist.Transform
elseif RigType == "R6" then
VMRootHip.Transform = RootHip.Transform
VMLeftShoulder.Transform = LeftShoulder.Transform
VMRightShoulder.Transform = RightShoulder.Transform
end
local function BuildCharacterToViewmodelPartMap()
table.clear(CharacterToViewmodelPart)
if RigType == "R15" then
CharacterToViewmodelPart[LowerTorso] = VMLowerTorso
CharacterToViewmodelPart[UpperTorso] = VMUpperTorso
CharacterToViewmodelPart[Root] = VMRoot
CharacterToViewmodelPart[LeftUpperArm] = VMLeftUpperArm
CharacterToViewmodelPart[LeftLowerArm] = VMLeftLowerArm
CharacterToViewmodelPart[LeftHand] = VMLeftHand
CharacterToViewmodelPart[RightUpperArm] = VMRightUpperArm
CharacterToViewmodelPart[RightLowerArm] = VMRightLowerArm
CharacterToViewmodelPart[RightHand] = VMRightHand
else
CharacterToViewmodelPart[Torso] = VMTorso
CharacterToViewmodelPart[HumanoidRootPart] = VMHumanoidRootPart
CharacterToViewmodelPart[RootHip] = VMRootHip
CharacterToViewmodelPart[LeftArm] = VMLeftArm
CharacterToViewmodelPart[RightArm] = VMRightArm
end
end
BuildCharacterToViewmodelPartMap()
end)
Humanoid.Died:Connect(function()
Disable()
end)
Humanoid.AncestryChanged:Connect(function()
if not Humanoid:IsDescendantOf(game) then
Disable()
end
end)
--Viewmodel.Parent = Camera
I know it’s long, but the main viewmodel logic is done, I just need to know how to make the tool attach logic
this would be the hierarchy :
If someone knows anything about viewmodels, let me know any suggestions or fixes, please
