ViewModel glitching? Works for one player but doesn't work for others

This script is from this tutorial
But i have tweaked it to make it fit my guns and game.

  1. What do you want to achieve?

I want to have my viewmodel working for other people (not just me/on my account) and make sure its not flinging/glitching.

  1. What is the issue?

Localscript in StarterCharacterScripts:

local RunService = game:GetService(“RunService”)
local PhysicsService = game:GetService(“PhysicsService”)

local Camera = workspace.CurrentCamera
if Camera:FindFirstChild(“ViewModel”) then
Camera.ViewModel:Destroy()
end

local Character = script.Parent
local Animator = Character:WaitForChild(“Humanoid”):WaitForChild(“Animator”)
local Offset = Instance.new(“CFrameValue”, script)
Offset.Name = “Offset”

Character.Archivable = true
local ViewModel = Character:Clone()
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
ViewModel:SetPrimaryPartCFrame(CFrame.new(0,5,0))

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.
end
end

for i,v in pairs(ViewModel:GetChildren()) do
if v.Name == “Hat” then
v:Destroy()
end
end

local function updateHead()
ViewModel.Head.CFrame = Camera.CFrame
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()
ViewModel.Head.CFrame = Camera.CFrame
ViewModel:SetPrimaryPartCFrame(Camera.CFrame:ToWorldSpace(Offset.Value))

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)

ViewModel.Parent = Camera

The guns appear to be at a certain angle (not facing the camera). But the arms are flinging/glitching around the map.

Screenshot of it working:

Screenshot on my other account (not working):

  1. What solutions have you tried so far?

Ive tried messing around with the SetPrimaryPartCFrame but yet still doesn’t make a difference. Ive also tried setting the orientation so it fits the camera.

Video (me selecting each part of the viewmodel (arms,torso,etc…)):
robloxapp-20210720-1955344.wmv
(1.8 MB)

This is also my first post! Any help is appreciated!

Maybe the issue is with the gun itself? Could you try equipping the same gun on both player clients?

1 Like

Sorry for the late reply. Would you mean equipping the gun into the viewmodel as well as the player who equipped it? Sorry im new to scripting.

Update:

Tried it on a new baseplate with a brand new gun (no scripts in it only the handle and the animations script) and it still has the same bugs (no tweaks the original script). I also forgot to mention that both the players are not in the same server. I tested them separately. I also noticed that when multiple clients are in the server the viewmodel doesn’t show up (the same way it happened on my alt).

This also happens as well but only on my alt not my main:
Screenshot (1)

Screenshot (2)

I fixed it but still don’t know why this happens?

Hopefully this extra info helps. :grinning:

I’ve never encountered this bug before, but my tutorial wasn’t really aimed to letting you copy-paste the script, it was more of an explanation on ViewModel basics so you make your own one, my script cannot support all games can it? I suggest you try and make your own script with what you learned from the tutorial, if you want to make a more advanced and cooler ViewModel I suggest seeing other tutorials since mine is just for basics.

Also what I meant in my first reply was if you could equip the exact same gun on both accounts.

Oh ok thanks for letting me know :sweat_smile:

I think I’ve encountered this bug before when the arm flinging etc. That’s because the viewmodel may not be anchored.

I have made a new viewmodel script, so I wont be posting on this topic anymore but thanks for the help guys. :grinning: