--Viewmodel


if Module.VMIdleAnimationID ~= nil then
	local VMIdleAnim = Instance.new("Animation",Tool)
	VMIdleAnim.Name = "VMIdleAnim"
	VMIdleAnim.AnimationId = "rbxassetid://"..Module.VMIdleAnimationID
end
if Module.VMFireAnimationID ~= nil then
	local VMFireAnim = Instance.new("Animation",Tool)
	VMFireAnim.Name = "VMFireAnim"
	VMFireAnim.AnimationId = "rbxassetid://"..Module.VMFireAnimationID
end
if Module.VMReloadAnimationID ~= nil then
	local VMReloadAnim = Instance.new("Animation",Tool)
	VMReloadAnim.Name = "VMReloadAnim"
	VMReloadAnim.AnimationId = "rbxassetid://"..Module.VMReloadAnimationID
end
if Module.VMShotgunClipinAnimationID ~= nil then
	local VMShotgunClipinAnim = Instance.new("Animation",Tool)
	VMShotgunClipinAnim.Name = "VMShotgunClipinAnim"
	VMShotgunClipinAnim.AnimationId = "rbxassetid://"..Module.VMShotgunClipinAnimationID
end
if Module.VMShotgunPumpinAnimationID ~= nil then
	local VMShotgunPumpinAnim = Instance.new("Animation",Tool)
	VMShotgunPumpinAnim.Name = "VMShotgunPumpinAnim"
	VMShotgunPumpinAnim.AnimationId = "rbxassetid://"..Module.VMShotgunPumpinAnimationID
end
if Module.VMHoldDownAnimationID ~= nil then
	local VMHoldDownAnim = Instance.new("Animation",Tool)
	VMHoldDownAnim.Name = "VMHoldDownAnim"
	VMHoldDownAnim.AnimationId = "rbxassetid://"..Module.VMHoldDownAnimationID
end
if Module.VMEquippedAnimationID ~= nil then
	local VMEquippedAnim = Instance.new("Animation",Tool)
	VMEquippedAnim.Name = "VMEquippedAnim"
	VMEquippedAnim.AnimationId = "rbxassetid://"..Module.VMEquippedAnimationID
end
if Module.SecondaryFireAnimationEnabled and Module.VMSecondaryFireAnimationID ~= nil then
	local VMSecondaryFireAnim = Instance.new("Animation",Tool)
	VMSecondaryFireAnim.Name = "VMSecondaryFireAnim"
	VMSecondaryFireAnim.AnimationId = "rbxassetid://"..Module.VMSecondaryFireAnimationID
end
if Module.VMSecondaryShotgunPump and Module.VMSecondaryShotgunPumpinAnimationID ~= nil then
	local VMSecondaryShotgunPumpinAnim = Instance.new("Animation",Tool)
	VMSecondaryShotgunPumpinAnim.Name = "VMSecondaryShotgunPumpinAnim"
	VMSecondaryShotgunPumpinAnim.AnimationId = "rbxassetid://"..Module.VMSecondaryShotgunPumpinAnimationID
end
if Module.TacticalReloadAnimationEnabled and Module.VMTacticalReloadAnimationID ~= nil then
	local VMTacticalReloadAnim = Instance.new("Animation",Tool)
	VMTacticalReloadAnim.Name = "VMTacticalReloadAnim"
	VMTacticalReloadAnim.AnimationId = "rbxassetid://"..Module.VMTacticalReloadAnimationID
end
if Module.InspectAnimationEnabled and Module.VMInspectAnimationID ~= nil then
	local VMInspectAnim = Instance.new("Animation",Tool)
	VMInspectAnim.Name = "VMInspectAnim"
	VMInspectAnim.AnimationId = "rbxassetid://"..Module.VMInspectAnimationID
end
if Module.ShotgunReload and Module.PreShotgunReload and Module.VMPreShotgunReloadAnimationID ~= nil then
	local VMPreShotgunReloadAnim = Instance.new("Animation",Tool)
	VMPreShotgunReloadAnim.Name = "VMPreShotgunReloadAnim"
	VMPreShotgunReloadAnim.AnimationId = "rbxassetid://"..Module.VMPreShotgunReloadAnimationID
end
if Module.VMMinigunRevUpAnimationID ~= nil then
	local VMMinigunRevUpAnim = Instance.new("Animation",Tool)
	VMMinigunRevUpAnim.Name = "VMMinigunRevUpAnim"
	VMMinigunRevUpAnim.AnimationId = "rbxassetid://"..Module.VMMinigunRevUpAnimationID
end
if Module.VMMinigunRevDownAnimationID ~= nil then
	local VMMinigunRevDownAnim = Instance.new("Animation",Tool)
	VMMinigunRevDownAnim.Name = "VMMinigunRevDownAnim"
	VMMinigunRevDownAnim.AnimationId = "rbxassetid://"..Module.VMMinigunRevDownAnimationID
end
if Module.ChargingAnimationEnabled and Module.VMChargingAnimationID ~= nil then
	local VMChargingAnim = Instance.new("Animation",Tool)
	VMChargingAnim.Name = "VMChargingAnim"
	VMChargingAnim.AnimationId = "rbxassetid://"..Module.VMChargingAnimationID
end
if Module.SelectiveFireEnabled and Module.VMSwitchAnimationID ~= nil then
	local VMSwitchAnim = Instance.new("Animation",Tool)
	VMSwitchAnim.Name = "VMSwitchAnim"
	VMSwitchAnim.AnimationId = "rbxassetid://"..Module.VMSwitchAnimationID
end
if Module.BatteryEnabled and Module.VMOverheatAnimationID ~= nil then
	local VMOverheatAnim = Instance.new("Animation",Tool)
	VMOverheatAnim.Name = "VMOverheatAnim"
	VMOverheatAnim.AnimationId = "rbxassetid://"..Module.VMOverheatAnimationID
end
if Module.MeleeAttackEnabled and Module.VMMeleeAttackAnimationID ~= nil then
	local VMMeleeAttackAnim = Instance.new("Animation")
	VMMeleeAttackAnim.Name = "VMMeleeAttackAnim"
	VMMeleeAttackAnim.AnimationId = "rbxassetid://"..Module.VMMeleeAttackAnimationID
	VMMeleeAttackAnim.Parent = Tool
end

ChangeMagAndAmmo.OnServerEvent:Connect(function(Player, Mag, Ammo, Heat)
	MagValue.Value = Mag
	AmmoValue.Value = Ammo
	HeatValue.Value = Heat
end)

InflictTargetMelee.OnServerInvoke = function(Player, ClientModule, TargetHumanoid, TargetTorso, Damage, Misc, Critical, Hit)
	_G.SecureSettings(Player, Tool, ClientModule) --Second layer
	local TrueDamage = (Hit and Hit.Name == "Head" and Damage[2]) and Damage[1] * Damage[3] or Damage[1]
	if Player then
		local TargetTEAM = TargetHumanoid.Parent:FindFirstChild("TEAM")
		local TEAM = Tool.Parent:FindFirstChild("TEAM")
		local CanDamage = (TEAM and TargetTEAM) and (TargetTEAM.Value ~= TEAM.Value) or DamageModule.CanDamage(TargetHumanoid.Parent, Player)
		if TargetHumanoid and TargetHumanoid.Health ~= 0 and TargetTorso and CanDamage then
			while TargetHumanoid:FindFirstChild("creator") do
				TargetHumanoid.creator:Destroy()
			end
			local creator = Instance.new("ObjectValue",TargetHumanoid)
			creator.Name = "creator"
			creator.Value = Player
			Debris:AddItem(creator, 5)
			if Critical[1] then
				local CriticalChanceRandom = Random.new():NextInteger(0, 100)
				if CriticalChanceRandom <= Critical[2] then
					TargetHumanoid:TakeDamage(math.abs(TrueDamage * Critical[3]))
				else
					TargetHumanoid:TakeDamage(math.abs(TrueDamage))
				end			
			else
				TargetHumanoid:TakeDamage(math.abs(TrueDamage))
			end
			if Misc[1] > 0 then --knockback
				local Shover = Character.HumanoidRootPart or Character.Head
				local Duration = 0.1
				local Speed = Misc[1] / Duration
				local Velocity = (TargetTorso.Position - Shover.Position).Unit * Speed
				local ShoveForce = Instance.new("BodyVelocity")
				ShoveForce.MaxForce = Vector3.new(1e9, 1e9, 1e9)
				ShoveForce.Velocity = Velocity
				ShoveForce.Parent = TargetTorso
				Debris:AddItem(ShoveForce, Duration)
			end
			if Misc[2] > 0 and Character.Humanoid and Character.Humanoid.Health ~= 0 then --lifesteal
				Character.Humanoid.Health = Character.Humanoid.Health + (TrueDamage * Misc[2])
			end
			if Misc[3] then	--affliction
				local roll = math.random(1, 100)
				if roll <= Misc[5] then
					if not TargetHumanoid.Parent:FindFirstChild(Misc[4]) then
						local Debuff = script:FindFirstChild(Misc[4]):Clone()
						Debuff.creator.Value = creator.Value
						Debuff.Parent = TargetHumanoid.Parent
						Debuff.Disabled = false
					end
				end
			end		
		end
	else
		warn("Unable to register damage because player is no longer existing here")
	end
end

Tool.Equipped:Connect(function()
	Player = Players:GetPlayerFromCharacter(Tool.Parent)
	Character = Tool.Parent
	Humanoid = Character:FindFirstChild("Humanoid")
	if Module.DualEnabled and Workspace.FilteringEnabled then
		Handle2.CanCollide = false
		local LeftArm = Tool.Parent:FindFirstChild("Left Arm") or Tool.Parent:FindFirstChild("LeftHand")
		local RightArm = Tool.Parent:FindFirstChild("Right Arm") or Tool.Parent:FindFirstChild("RightHand")
		if RightArm then
			local Grip = RightArm:WaitForChild("RightGrip", 0.01)
			if Grip then
				Grip2 = Grip:Clone()
				Grip2.Name = "LeftGrip"
				Grip2.Part0 = LeftArm
				Grip2.Part1 = Handle2
				--Grip2.C1 = Grip2.C1:inverse()
				Grip2.Parent = LeftArm
			end
		end
	end
end)

Tool.Unequipped:Connect(function()
	if Module.DualEnabled and Workspace.FilteringEnabled then
		Handle2.CanCollide = true
		if Grip2 then
			Grip2:Destroy()
		end
	end
end)