--------------------[ RELOAD FUNCTIONS ]----------------------------------------------



function ReloadAnim()
	TweenJoint(LWeld2, CF(), CF(), Sine, 0.15)
	TweenJoint(RWeld2, CF(), CF(), Sine, 0.15)
	local Speed = S.ReloadTime / 2
	local Mag_Parts = {}
			
	for _, Obj in pairs(Gun:GetChildren()) do
		if Obj.Name == "Mag" and Obj:IsA("BasePart") then
			INSERT(Mag_Parts, {Original = Obj, Clone1 = Obj:Clone(), Clone2 = Obj:Clone()})
		end
	end
	
	local W1 = nil
	local W2 = nil
	
	local SequenceTable = {
		
		function()
			
			for Index, Mag in pairs(Mag_Parts) do
				Mag.Original.Transparency = 1
				Mag.Clone1.Parent = Gun_Ignore
				Mag.Clone1.CanCollide = true
				
				if Index ~= 1 then
					local W = Instance.new("Weld")
					W.Part0 = Mag_Parts[1].Clone1
					W.Part1 = Mag.Clone1
					W.C0 = Mag_Parts[1].Clone1.CFrame:toObjectSpace(Mag.Clone1.CFrame)
					W.Parent = Mag_Parts[1].Clone1
				end
			end
			
			W1 = Instance.new("Weld")
			W1.Part0 = Mag_Parts[1].Clone1
			W1.Part1 = Handle
			W1.C0 = Mag_Parts[1].Original.CFrame:toObjectSpace(Handle.CFrame)
			W1.Parent = Mag_Parts[1].Clone1
			
			TweenJoint(LWeld, ArmC0[1], CF(0, 0.61, 0) * CFANG(RAD(70), 0, 0), Linear, 0.5 * Speed)
			TweenJoint(RWeld, ArmC0[2], CF(0.4, 0.09, -0.21) * CFANG(RAD(20), RAD(3), 0), Linear, 0.35 * Speed)
			TweenJoint(Grip, Grip.C0, CFANG(0, RAD(10), 0), Linear, 0.5 * Speed)
			wait(0.3 * Speed)
			
		end;
		
		function()
			
			TweenJoint(RWeld, ArmC0[2], CF(0.4, -0.01, -0.31) * CFANG(RAD(-22), RAD(3), 0), Sine, 0.3 * Speed)
			wait(0.2 * Speed)
			
		end;
		
		function()
			
			W1:Destroy()
			Mag_Parts[1].Clone1.Velocity = Handle.Velocity + Handle.CFrame:vectorToWorldSpace(VEC3(0,-5,0)) * 20
			spawn(function()
				while Mag_Parts[1].Clone1.Velocity.magnitude > 0.1 do wait() end
				for _, Mag in pairs(Mag_Parts) do
					Mag.Clone1.Anchored = true
					Mag.Clone1:BreakJoints()
				end
			end)
			
			for Index, Mag in pairs(Mag_Parts) do
				Mag.Clone2.Parent = Gun_Ignore
				
				if Index ~= 1 then
					local W = Instance.new("Weld")
					W.Part0 = Mag_Parts[1].Clone2
					W.Part1 = Mag.Clone2
					W.C0 = Mag_Parts[1].Clone2.CFrame:toObjectSpace(Mag.Clone2.CFrame)
					W.Parent = Mag_Parts[1].Clone2
				end
			end
			
			W2 = Instance.new("Weld")
			W2.Part0 = FakeLArm
			W2.Part1 = Mag_Parts[1].Clone2
			W2.C0 = CF(0, -1, 0) * CFANG(RAD(-90), 0, 0)
			W2.Parent = FakeLArm
			wait(0.1)
			
		end;
		
		function()
			
			local FakeLArmCF = LWeld.Part0.CFrame * ArmC0[1] * (CF(0.3, 1.9, -0.31) * CFANG(RAD(-20), RAD(30), RAD(-60))):inverse()
			local FakeRArmCF = RWeld.Part0.CFrame * ArmC0[2] * (CF(0.4, -0.1, -0.21) * CFANG(RAD(-20), RAD(5), RAD(10))):inverse()
			local HandleCF = FakeRArm.CFrame:toObjectSpace(Grip.Part0.CFrame * Grip.C0)
			local Mag_Original_CF = Handle.CFrame:toObjectSpace(Mag_Parts[1].Original.CFrame)
			local MagC0 = FakeLArmCF:toObjectSpace(FakeRArmCF * HandleCF * Mag_Original_CF)
			
			TweenJoint(LWeld, ArmC0[1], CF(-0.3, 1.85, -0.11) * CFANG(RAD(-10), RAD(30), RAD(-60)), Sine, 0.6 * Speed)
			TweenJoint(RWeld, ArmC0[2], CF(0.4, -0.1, -0.21) * CFANG(RAD(-20), RAD(5), RAD(10)), Sine, 0.6 * Speed)
			TweenJoint(Grip, Grip.C0, CF(), Sine, 0.6 * Speed)
			TweenJoint(W2, MagC0, CF(), Sine, 0.6 * Speed)
			wait(0.7 * Speed)
			
		end;
		
		function()
			
			for _, Mag in pairs(Mag_Parts) do
				Mag.Original.Transparency = 0
				Mag.Clone2:Destroy()
			end
			
			TweenJoint(LWeld, ArmC0[1], S.ArmC1_UnAimed.Left, Sine, 0.5 * Speed)
			TweenJoint(RWeld, ArmC0[2], S.ArmC1_UnAimed.Right, Sine, 0.5 * Speed)
			TweenJoint(Grip, Grip.C0, CFANG(0, RAD(20), 0), Sine, 0.5 * Speed)
			wait(0.5 * Speed)
			
		end;
		
	}
	
	for _,ReloadFunction in pairs(SequenceTable) do
		if BreakReload then
			break
		end
		ReloadFunction()
	end
	
	if W1 then W1:Destroy() end
	if W2 then W2:Destroy() end
	for _, Mag in pairs(Mag_Parts) do
		Mag.Clone1:Destroy()
		Mag.Clone2:Destroy()
	end
end

function Reload()
	Running = false
	if Ammo.Value < ClipSize.Value and (not Reloading) and StoredAmmo.Value > 0 then
		AmmoInClip = (AmmoInClip == 0 and Ammo.Value or AmmoInClip)
		Ammo.Value = 0
		Reloading = true
		if Aimed then UnAimGun(S.ReloadAnimation) end
		Gui_Clone.CrossHair.Reload.Visible = true
		if Handle:FindFirstChild("ReloadSound") then Handle.ReloadSound:Play() end
		if S.ReloadAnimation then
			wait()
			ReloadAnim()
		else
			local StartReload = tick()
			while true do
				if BreakReload then break end
				if (tick() - StartReload) >= S.ReloadTime then break end
				RS:wait()
			end
		end
		if (not BreakReload) then
			if StoredAmmo.Value >= ClipSize.Value then
				Ammo.Value = ClipSize.Value
				if AmmoInClip > 0 then
					StoredAmmo.Value = StoredAmmo.Value - (ClipSize.Value - AmmoInClip)
				else
					StoredAmmo.Value = StoredAmmo.Value - ClipSize.Value
				end
			elseif StoredAmmo.Value < ClipSize.Value and StoredAmmo.Value > 0 then
				Ammo.Value = StoredAmmo.Value
				StoredAmmo.Value = 0
			end
		end
		BreakReload = false
		Reloading = false
		if Selected then
			AmmoInClip = 0
			Gui_Clone.CrossHair.Reload.Visible = false
		end
	end
end