You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
working viewmodels
- What is the issue? Include screenshots / videos if possible!
the viewmodels disappear randomly then appear again
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
looked for solutions but didnt find much help
the viewmodel code: (used rokoblox5 guide)
local RunService = game:GetService("RunService")
local PhysicsService = game:GetService("PhysicsService")
local SpringModule = require(game.ReplicatedStorage.SpringModule)
local Camera = workspace.CurrentCamera
if Camera:FindFirstChild("ViewModel") then
Camera.ViewModel:Destroy() -- // Since the script reloads everytime the player respawns, we have to delete old ViewModels from the camera.
end
local Character = script.Parent
local Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator") -- // We need the animator to replicate animations to the ViewModel
Character.Archivable = true -- // If Archivable is false (Which the character has it false by default), we cannot clone it, until we set it to true.
local ViewModel = Character:Clone()
ViewModel.Parent = Camera
ViewModel.Name = "ViewModel"
Character.Archivable = false
local ViewModelAnimator = ViewModel.Humanoid.Animator -- // Used to play animations on the ViewModel.
ViewModel.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None -- // Disable name display.
ViewModel.Humanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff -- // Disable health display.
ViewModel.Humanoid.BreakJointsOnDeath = false
ViewModel.Head.Anchored = true
ViewModel.PrimaryPart = ViewModel.Head
local Offset = Instance.new("CFrameValue", script)
Offset.Name = "Offset"
local mouse = game.Players.LocalPlayer:GetMouse()
-- // Now we have to delete the legs since we won't use them and disable the ViewModel's shadow.
for _, Part in pairs(ViewModel:GetDescendants()) do
if Part:IsA("BasePart") then
Part.CastShadow = false -- // If it's a part, then disable it's shadow.
PhysicsService:SetPartCollisionGroup(Part, "ViewModel")
local LowerName = Part.Name:lower() -- // Get the lowercase part name
if LowerName:match("leg") or LowerName:match("foot") then
Part:Destroy() -- // If this is a leg/foot part then delete it, since it's useless.
elseif not (LowerName:match("arm") or LowerName:match("hand")) then
Part.Transparency = 1 -- // If this part isn't a part of the arms then it should be invisible.
end
elseif Part:IsA("Decal") then
Part:Destroy() -- // Delete all decals (Face).
elseif Part:IsA("Accessory") then
Part:Destroy() -- // Delete all accessories.
elseif Part:IsA("LocalScript") then
Part:Destroy() -- // Destroy all scripts.
elseif Part:IsA("ForceField") then
Part:Destroy()
end
end
local LoadedAnimations = {}
Animator.AnimationPlayed:Connect(function(AnimationTrack)
if AnimationTrack:GetAttribute("ViewModelAnimation") ~= true then return end -- // Skip animation if it isn't supposed to play on ViewModel.
if not LoadedAnimations[AnimationTrack] then -- // Indexing using the animation track.
-- // If this animation was not already laoded then load it.
LoadedAnimations[AnimationTrack] = ViewModelAnimator:LoadAnimation(AnimationTrack.Animation) -- // Load animation on the ViewModel.
end
end)
local function updateAnimations()
for CharAnim, Anim in pairs(LoadedAnimations) do
if CharAnim.IsPlaying ~= Anim.IsPlaying then
if CharAnim.IsPlaying then
Anim:Play()
else
Anim:Stop()
end
end
Anim.TimePosition = CharAnim.TimePosition
Anim:AdjustWeight(CharAnim.WeightCurrent, 0) -- // 0 Fade time so it's instantly set.
end
end
RunService.RenderStepped:Connect(function(dl)
updateAnimations()
local GAH = Camera.CFrame:ToWorldSpace(Offset.Value) * CFrame.new(0, -0.5, 0)
ViewModel.PrimaryPart.CFrame = ViewModel.PrimaryPart.CFrame:Lerp(GAH, 1/3.5)
local ViewModelShirt = ViewModel:FindFirstChildWhichIsA("Shirt") or Instance.new("Shirt", ViewModel) -- // Create a shirt if there is no shirt found.
local CharacterShirt = Character:FindFirstChildWhichIsA("Shirt")
if CharacterShirt then
-- // If a shirt was found in the player's character, then set the ViewModel's shirt to the same shirt.
ViewModelShirt.ShirtTemplate = CharacterShirt.ShirtTemplate
end
for _, Part in pairs(ViewModel:GetChildren()) do
if Part:IsA("BasePart") then
-- // Set the color of each part of the ViewModel to the color of the part with same name in the character.
Part.Color = Character[Part.Name].Color
end
end
end)
Character.DescendantAdded:Connect(function(Obj)
if Obj:IsA("Weld") and Obj.Name == "RightGrip" then
wait()
Obj.Part0 = ViewModel[Obj.Part0.Name]
end
end)
viewmodels bugging out video: