Gun viewmodel fling/fly glitch

I made a viewmodel for my weapon.
The problem is that if you look down and take the weapon, the player will fly up (although viewmodel collision is completely disabled). But if you take the weapon and then look down, the player will not fly up.

I tried to write in the script that it would disable viewmodel collision itself, but the result was the same. and I also tried to move viewmodel in different directions from the player’s camera. but it just changes the side in which the player will fly.

viewmodel collision is disabled(gun and hands)

local idle_id = 139001370855610
local gun = script.Parent
local eq = false
local track
gun.Equipped:Connect(function()
	eq = true
	script.Parent.Parent:FindFirstChild('Right Arm'):FindFirstChild('RightGrip'):Destroy()

	local humanoid = gun.Parent:FindFirstChild('Humanoid')
	local anim = Instance.new('Animation')
	anim.Parent = script

	anim.AnimationId =  'rbxassetid://'.. idle_id
	track = humanoid:LoadAnimation(anim)

	track:Play()
end)
gun.Unequipped:Connect(function()
	track:Stop()
	eq = false
end)
local in_first = false
local cloned = false
game:GetService('RunService').RenderStepped:Connect(function()

	if eq == true then
		local rs = game.Players.LocalPlayer.Character:FindFirstChild('Torso'):FindFirstChild('Right Shoulder')
		rs.C0 = CFrame.new(rs.C0.Position) * CFrame.Angles(workspace.CurrentCamera.CFrame.LookVector.Y,(math.rad(90)),0)
	else
		local rs = game.Players.LocalPlayer.Character:FindFirstChild('Torso'):FindFirstChild('Right Shoulder')
		rs.C0 = CFrame.new(rs.C0.Position) * CFrame.Angles(0,math.rad(90),0)
	end
	local camera = workspace.CurrentCamera
	if (game.Players.LocalPlayer.Character.Head.CFrame.p - camera.CFrame.p).magnitude < 1 and gun.Parent == game.Players.LocalPlayer.Character then
		in_first = true
		if cloned == false then
			viewmodel = game.ReplicatedStorage.m4_vm:Clone()
			cloned = true
			viewmodel.Parent = workspace
			local id = 133561721907561
			local anim = Instance.new('Animation')
			anim.Parent = viewmodel
			anim.AnimationId =  'rbxassetid://'..id
			local humanoid = viewmodel:FindFirstChild('Humanoid')
			local anim_track = humanoid:LoadAnimation(anim)
			anim_track:Play()
			for i,v in pairs(gun:GetChildren()) do
				if not v:IsA('LocalScript') and not v:IsA('Model') then
					
					v.Transparency = 1
					v.CanCollide = false
				end 
				if v:IsA('Model') then
					for z,x in pairs(v:GetChildren()) do
						if not x:IsA('LocalScript') then
							x.Transparency = 1
							x.CanCollide = false
						end
					end
				end
			end
		end
		viewmodel:SetPrimaryPartCFrame(camera.CFrame * CFrame.new(-0.5,-0.8, -1.5) * CFrame.Angles(0,0,0))
	else
		in_first = false
		local z,c = pcall(function()
			cloned = false
			viewmodel:Destroy()
			for i,v in pairs(gun:GetChildren()) do
				if not v:IsA('LocalScript') and not v:IsA('Model') then

					v.Transparency = 0
					v.CanCollide = false
				end 
				if v:IsA('Model') then
					for z,x in pairs(v:GetChildren()) do
						if not x:IsA('LocalScript') then
							x.Transparency = 0
							x.CanCollide = false
						end
					end
				end
			end
		end)
	end
end)

Here’s my (viewmodel)code if you want. On line 51 I wrote that the collision would be disabled. But as I wrote before nothing changed.

video:https://drive.google.com/file/d/11bFUwcWR8rmU1GnccX5VsLRApKXjo8Dt/view?usp=drive_link

To be 100% safe make a separate uncolidable collision group for a model i guess…
Also use Animator to create animationtrack plz.
Also since you are already making a closure bro wth code is it?
image
Dude code is awful
It is likely a problem to begin with
Also pairs is not needed in Luau anymore aswell as ipairs

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.