function animateReload()tweenJoint(LWeld2,CF(),CF(),Sine,.15)tweenJoint(RWeld2,CF(),CF(),Sine,.15)local t={}local s={}for s,P in pairs(Gun:GetChildren())do if string.sub(P.Name,1,3)=="\077\097\103"and P:IsA("\066\097\115\101\080\097\114\116")then INSERT(t,P)end end local P={tweenJoint=tweenJoint;makeMagInvisible=function()for t,s in pairs(t)do s.Transparency=1 end magVisible=false end;makeMagVisible=function()for t,s in pairs(t)do s.Transparency=(s:WaitForChild("\109\097\103\084\114\097\110\115")).Value end magVisible=true end;isMagVisible=function()return magVisible end;isMagEmpty=function()return ammoInClip==0 end,setNewMag=function()newMag=true end;isNewMag=function()return newMag end,createMag=function(P)local G=Instance.new("\077\111\100\101\108")local W={}for t,s in pairs(t)do local P=s:Clone()P.Transparency=(s:WaitForChild("\109\097\103\084\114\097\110\115")).Value P.CanCollide=false P.Parent=G INSERT(W,{Original=s,magClone=P})if t~=1 then local t=Instance.new("\087\101\108\100")t.Part0=W[1].magClone t.Part1=P t.C0=W[1].magClone.CFrame:toObjectSpace(P.CFrame)t.Parent=W[1].magClone end end s[P]={G;W}return G,W end;getMag=function(t)if s[t]then return s[t][1],s[t][2]else return nil,nil end end,attachGripToHead=function()local t=RArm.CFrame*Grip.C0 Grip.C0=Head.CFrame:toObjectSpace(t)Grip.Part0=Head end;attachGripToArm=function()local t=Head.CFrame*Grip.C0 Grip.C0=RArm.CFrame:toObjectSpace(t)Grip.Part0=RArm end,Sine=Sine,Linear=Linear,Handle=Handle;LArm=LArm,RArm=RArm,LWeld=LWeld;RWeld=RWeld,LC0=armC0[1];RC0=armC0[2],Grip=Grip,gunIgnore=gunIgnore;Cam=Cam;CF=CF;CFANG=CFANG;V3=V3,RAD=RAD,reloadTimeLoaded=S.reloadSettings.Times.Loaded,reloadTimeEmpty=S.reloadSettings.Times.Empty}local G=Anims.Reload(P)for t,s in pairs(G)do if breakReload then break end s()if not magVisible then Ammo.Value=0 end updateClipAmmo()end if not isCrawling then if Running and not S.canFireWhileRunning then tweenJoint(LWeld,armC0[1],S.runningC1.leftArm,Sine,.4)tweenJoint(RWeld,armC0[2],S.runningC1.rightArm,Sine,.4)tweenJoint(Grip,nil,S.runningC1.Grip,Sine,.4)else tweenJoint(LWeld,armC0[1],S.unAimedC1.leftArm,Sine,.4)tweenJoint(RWeld,armC0[2],S.unAimedC1.rightArm,Sine,.4)tweenJoint(Grip,nil,S.unAimedC1.Grip,Sine,.4)end end for t,s in pairs(s)do s[1]:Destroy()end end function Reload()if Ammo.Value<ClipSize.Value+1 and(not Reloading and StoredAmmo.Value>0)then Firing=false ammoInClip=ammoInClip==0 and Ammo.Value or ammoInClip Reloading=true lowerSpread()if Aimed then unAimGun(S.reloadSettings.Anim)end crossHair.Reload.Visible=true if Handle:FindFirstChild("\082\101\108\111\097\100\083\111\117\110\100")then Handle.ReloadSound:Play()end if S.reloadSettings.Anim then wait()animateReload()else local t=tick()local s=Ammo.Value==0 and S.reloadSettings.Times.Empty or S.reloadSettings.Times.Loaded while true do if breakReload then break end if tick()-t>=s then break end RS.RenderStepped:wait()end end if not breakReload then newMag=false if StoredAmmo.Value>=ClipSize.Value then if ammoInClip>0 then StoredAmmo.Value=StoredAmmo.Value-((ClipSize.Value+1)-ammoInClip)Ammo.Value=ClipSize.Value+1 else StoredAmmo.Value=StoredAmmo.Value-ClipSize.Value Ammo.Value=ClipSize.Value end elseif StoredAmmo.Value<ClipSize.Value and StoredAmmo.Value>0 then Ammo.Value=StoredAmmo.Value StoredAmmo.Value=0 end end Reloading=false if Selected then ammoInClip=breakReload and ammoInClip or 0 crossHair.Reload.Visible=false end breakReload=false end updateClipAmmo()updateStoredAmmo()end
--------------------[ RELOAD FUNCTIONS ]----------------------------------------------

function animateReload()
	tweenJoint(LWeld2, CF(), CF(), Sine, 0.15)
	tweenJoint(RWeld2, CF(), CF(), Sine, 0.15)
	local magParts = {}
	local magTable = {}

	for _, Obj in pairs(Gun:GetChildren()) do
		if string.sub(Obj.Name, 1, 3) == "Mag" and Obj:IsA("BasePart") then
			INSERT(magParts, Obj)
		end
	end

	local animVars = {
		--FUNCTIONS--
		tweenJoint = tweenJoint,

		makeMagInvisible = function()
			for _, v in pairs(magParts) do
				v.Transparency = 1
			end
			magVisible = false
		end,

		makeMagVisible = function()
			for _, v in pairs(magParts) do
				v.Transparency = v:WaitForChild("magTrans").Value
			end
			magVisible = true
		end,

		isMagVisible = function()
			return magVisible
		end,

		isMagEmpty = function()
			return ammoInClip == 0
		end,

		setNewMag = function()
			newMag = true
		end,

		isNewMag = function()
			return newMag
		end,

		createMag = function(Key)
			local magModel = Instance.new("Model")
			local magClones = {}
			for i, v in pairs(magParts) do
				local vClone = v:Clone()
				vClone.Transparency = v:WaitForChild("magTrans").Value
				vClone.CanCollide = false
				vClone.Parent = magModel
				INSERT(magClones, { Original = v, magClone = vClone })
				if i ~= 1 then
					local W = Instance.new("Weld")
					W.Part0 = magClones[1].magClone
					W.Part1 = vClone
					W.C0 = magClones[1].magClone.CFrame:toObjectSpace(vClone.CFrame)
					W.Parent = magClones[1].magClone
				end
			end
			magTable[Key] = { magModel, magClones }
			return magModel, magClones
		end,

		getMag = function(Key)
			if magTable[Key] then
				return magTable[Key][1], magTable[Key][2]
			else
				return nil, nil
			end
		end,

		attachGripToHead = function()
			local handleCF = RArm.CFrame * Grip.C0
			Grip.C0 = Head.CFrame:toObjectSpace(handleCF)
			Grip.Part0 = Head
		end,

		attachGripToArm = function()
			local handleCF = Head.CFrame * Grip.C0
			Grip.C0 = RArm.CFrame:toObjectSpace(handleCF)
			Grip.Part0 = RArm
		end,

		Sine = Sine,

		Linear = Linear,

		--VARIABLES--
		Handle = Handle,
		LArm = LArm,
		RArm = RArm,
		LWeld = LWeld,
		RWeld = RWeld,
		LC0 = armC0[1],
		RC0 = armC0[2],
		Grip = Grip,
		gunIgnore = gunIgnore,
		Cam = Cam,
		CF = CF,
		CFANG = CFANG,
		V3 = V3,
		RAD = RAD,
		reloadTimeLoaded = S.reloadSettings.Times.Loaded,
		reloadTimeEmpty = S.reloadSettings.Times.Empty,
	}

	local sequenceTable = Anims.Reload(animVars)
	--local T = tick()
	for _, reloadFunction in pairs(sequenceTable) do
		if breakReload then
			break
		end
		reloadFunction()

		if not magVisible then
			Ammo.Value = 0
		end
		updateClipAmmo()
	end
	--print(tick() - T) --I divide the reloadTime by this number to get the animation speed

	if not isCrawling then
		if Running and not S.canFireWhileRunning then
			tweenJoint(LWeld, armC0[1], S.runningC1.leftArm, Sine, 0.4)
			tweenJoint(RWeld, armC0[2], S.runningC1.rightArm, Sine, 0.4)
			tweenJoint(Grip, nil, S.runningC1.Grip, Sine, 0.4)
		else
			tweenJoint(LWeld, armC0[1], S.unAimedC1.leftArm, Sine, 0.4)
			tweenJoint(RWeld, armC0[2], S.unAimedC1.rightArm, Sine, 0.4)
			tweenJoint(Grip, nil, S.unAimedC1.Grip, Sine, 0.4)
		end
	end

	for _, v in pairs(magTable) do --In case the reload animations was stopped mid way and there were still fake mags that weren't deleted
		v[1]:Destroy()
	end
end

function Reload()
	if Ammo.Value < (ClipSize.Value + 1) and not Reloading and StoredAmmo.Value > 0 then
		Firing = false
		ammoInClip = (ammoInClip == 0 and Ammo.Value or ammoInClip)
		Reloading = true
		lowerSpread()
		if Aimed then
			unAimGun(S.reloadSettings.Anim)
		end
		crossHair.Reload.Visible = true
		if Handle:FindFirstChild("ReloadSound") then
			Handle.ReloadSound:Play()
		end
		if S.reloadSettings.Anim then
			wait()
			animateReload()
		else
			local startReload = tick()
			local initialReloadTime = Ammo.Value == 0 and S.reloadSettings.Times.Empty or S.reloadSettings.Times.Loaded
			while true do
				if breakReload then
					break
				end
				if (tick() - startReload) >= initialReloadTime then
					break
				end
				RS.RenderStepped:wait()
			end
		end
		if not breakReload then
			newMag = false
			if StoredAmmo.Value >= ClipSize.Value then
				if ammoInClip > 0 then
					StoredAmmo.Value = StoredAmmo.Value - ((ClipSize.Value + 1) - ammoInClip)
					Ammo.Value = ClipSize.Value + 1
				else
					StoredAmmo.Value = StoredAmmo.Value - ClipSize.Value
					Ammo.Value = ClipSize.Value
				end
			elseif StoredAmmo.Value < ClipSize.Value and StoredAmmo.Value > 0 then
				Ammo.Value = StoredAmmo.Value
				StoredAmmo.Value = 0
			end
		end
		Reloading = false
		if Selected then
			ammoInClip = (breakReload and ammoInClip or 0)
			crossHair.Reload.Visible = false
		end
		breakReload = false
	end

	updateClipAmmo()
	updateStoredAmmo()
end
