Help on the default roblox gun system

I was modifying the default gun system by a lot when I encountered a problem with the animation glitching out. I recently made a post about this but I decided it wasn’t that big of a deal but now it seems to glitch more.
robloxapp-20210730-0047121.wmv (1.2 MB)
As you can see when he is aiming/shooting the animation glitches. I was wondering if there was any way I could fix this.
Here is the following code to the aiming animation

function BulletWeapon:onRenderStepped(dt)
	local aimTrack = self:getAnimTrack(self:getConfigValue("AimTrack", "RifleAim"))
	local aimZoomTrack = self:getAnimTrack(self:getConfigValue("AimZoomTrack", "RifleAimDownSights"))
	if 1==1 then -- <--Ignore this
		BaseWeapon.onRenderStepped(self, dt)
		if not self.tipAttach then return end
		if not self.equipped then return end

		local tipCFrame = self.tipAttach.WorldCFrame

		if self.player == Players.LocalPlayer then
			-- Retrieve aim point from camera and update player's aim animation
			if aimTrack then
				local aimDir = tipCFrame.LookVector

				local gunLookRay = Ray.new(tipCFrame.p, aimDir * 500)

				local _, gunHitPoint = Roblox.penetrateCast(gunLookRay, self.ignoreList)

				if self.weaponsSystem.aimRayCallback then
					local _, hitPoint = Roblox.penetrateCast(self.weaponsSystem.aimRayCallback(), self.ignoreList)
					self.aimPoint = hitPoint
				else
					self.aimPoint = gunHitPoint
				end

				if not aimTrack.IsPlaying and not self.reloading then
					aimTrack:Play()
					coroutine.wrap(function() -- prevent player from firing until gun is fully out
						wait(self:getConfigValue("StartupTime", 0.2))
						self.startupFinished = true
					end)()
				end

				if aimZoomTrack and not self.reloading then
					if not aimZoomTrack.IsPlaying then
						aimZoomTrack:Play()
					end
					aimZoomTrack:AdjustSpeed(0.001)
					if self.weaponsSystem.camera:isZoomed() then
						if aimTrack.WeightTarget ~= 0 then
							aimZoomTrack:AdjustWeight(1)
							aimTrack:AdjustWeight(0)
						end
					elseif aimTrack.WeightTarget ~= 1 then
						aimZoomTrack:AdjustWeight(0)
						aimTrack:AdjustWeight(1)
					end
				end

				local MIN_ANGLE = -80
				local MAX_ANGLE = 80
				local aimYAngle = math.deg(self.recoilIntensity)
				if self.weaponsSystem.camera.enabled then
					-- Gets pitch and recoil from camera to figure out how high/low to aim the gun
					aimYAngle = math.deg(self.weaponsSystem.camera:getRelativePitch() + self.weaponsSystem.camera.currentRecoil.Y + self.recoilIntensity)
				end
				local aimTimePos = 2 * ((aimYAngle - MIN_ANGLE) / (MAX_ANGLE - MIN_ANGLE))

				aimTrack:AdjustSpeed(0.001)
				aimTrack.TimePosition = math.clamp(aimTimePos, 0.001, 1.97)

				if aimZoomTrack then
					aimZoomTrack.TimePosition = math.clamp(aimTimePos, 0.001, 1.97)
				end

				-- Update recoil (decay over time)
				local recoilDecay = self:getConfigValue("RecoilDecay", 0.825)
				self.recoilIntensity = math.clamp(self.recoilIntensity * recoilDecay, 0, math.huge)
			else
				warn("no aimTrack")
			end
		end
    end
end
1 Like