----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------


local BulletModel =  ACS_Storage.Server


game.Players.PlayerAdded:connect(function(SKP_001)	
SKP_001.CharacterAppearanceLoaded:Connect(function(SKP_002)

	for SKP_003, SKP_004 in pairs(Engine.Essential:GetChildren()) do
			if SKP_004 then
				local SKP_005 = SKP_004:clone()
				SKP_005.Parent = SKP_001.PlayerGui
				SKP_005.Disabled = false
			end
		end

	if SKP_001.Character:FindFirstChild('Head') then
		Evt.TeamTag:FireAllClients(SKP_001, SKP_002)
	end
end)

	SKP_001.Changed:connect(function()
		Evt.TeamTag:FireAllClients(SKP_001)
	end)

end)

function Weld(SKP_001, SKP_002, SKP_003, SKP_004)
	local SKP_005 = Instance.new("Motor6D", SKP_001)
	SKP_005.Part0 = SKP_001
	SKP_005.Part1 = SKP_002
	SKP_005.Name = SKP_001.Name
	SKP_005.C0 = SKP_003 or SKP_001.CFrame:inverse() * SKP_002.CFrame
	SKP_005.C1 = SKP_004 or CFrame.new()
	return SKP_005
end


Evt.Recarregar.OnServerEvent:Connect(function(Player, StoredAmmo,Arma)
	Arma.ACS_Modulo.Variaveis.StoredAmmo.Value = StoredAmmo
end)

Evt.Treino.OnServerEvent:Connect(function(Player, Vitima)

	if Vitima.Parent:FindFirstChild("Saude") ~= nil then
	local saude = Vitima.Parent.Saude
		saude.Variaveis.HitCount.Value = saude.Variaveis.HitCount.Value + 1
	end

end)

Evt.SVFlash.OnServerEvent:Connect(function(Player,Mode,Arma,Angle,Bright,Color,Range)
if ServerConfig.ReplicatedFlashlight then
	Evt.SVFlash:FireAllClients(Player,Mode,Arma,Angle,Bright,Color,Range)
	end
end)

Evt.SVLaser.OnServerEvent:Connect(function(Player,Position,Modo,Cor,Arma,IRmode)
	if ServerConfig.ReplicatedLaser then
	Evt.SVLaser:FireAllClients(Player,Position,Modo,Cor,Arma,IRmode)
	end
	--print(Player,Position,Modo,Cor)
end)

Evt.Breach.OnServerEvent:Connect(function(Player,Mode,BreachPlace,Pos,Norm,Hit)
	
	if Mode == 1 then
	Player.Character.Saude.Kit.BreachCharges.Value = Player.Character.Saude.Kit.BreachCharges.Value - 1
	BreachPlace.Destroyed.Value = true
	local C4 = Engine.FX.BreachCharge:Clone()

	C4.Parent = BreachPlace.Destroyable
	C4.Center.CFrame = CFrame.new(Pos, Pos + Norm) * CFrame.Angles(math.rad(-90),math.rad(0),math.rad(0))
	C4.Center.Place:play()

	local weld = Instance.new("WeldConstraint")
	weld.Parent = C4
	weld.Part0 = BreachPlace.Destroyable.Charge
	weld.Part1 = C4.Center

	wait(1)
	C4.Center.Beep:play()
	wait(4)
	local Exp = Instance.new("Explosion")
		Exp.BlastPressure = 0
		Exp.BlastRadius = 0
		Exp.DestroyJointRadiusPercent = 0
		Exp.Position = C4.Center.Position
		Exp.Parent = workspace
	
	local S = Instance.new("Sound")
		S.EmitterSize = 50
		S.MaxDistance = 1500
		S.SoundId = "rbxassetid://".. Explosion[math.random(1, 7)]
		S.PlaybackSpeed = math.random(30,55)/40
		S.Volume = 2
		S.Parent = Exp
		S.PlayOnRemove = true
		S:Destroy()

	--[[for SKP_001, SKP_002 in pairs(game.Players:GetChildren()) do
		if SKP_002:IsA('Player') and SKP_002.Character and SKP_002.Character:FindFirstChild('Head') and (SKP_002.Character.Head.Position - C4.Center.Position).magnitude <= 15 then
			local DistanceMultiplier = (((SKP_002.Character.Head.Position - C4.Center.Position).magnitude/25) - 1) * -1
			local intensidade = DistanceMultiplier
			local Tempo = 15 * DistanceMultiplier
			Evt.Suppression:FireClient(SKP_002,2,intensidade,Tempo)
		end
	end	]]

	Debris:AddItem(BreachPlace.Destroyable,0)
	elseif Mode == 2 then

	Player.Character.Saude.Kit.BreachCharges.Value = Player.Character.Saude.Kit.BreachCharges.Value - 1
	BreachPlace.Destroyed.Value = true
	local C4 = Engine.FX.BreachCharge:Clone()

	C4.Parent = BreachPlace
	C4.Center.CFrame = CFrame.new(Pos, Pos + Norm) * CFrame.Angles(math.rad(-90),math.rad(0),math.rad(0))
	C4.Center.Place:play()

	local weld = Instance.new("WeldConstraint")
	weld.Parent = C4
	weld.Part0 = BreachPlace.Door.Door
	weld.Part1 = C4.Center

	wait(1)
	C4.Center.Beep:play()
	wait(4)
	local Exp = Instance.new("Explosion")
		Exp.BlastPressure = 0
		Exp.BlastRadius = 0
		Exp.DestroyJointRadiusPercent = 0
		Exp.Position = C4.Center.Position
		Exp.Parent = workspace
	
	local S = Instance.new("Sound")
		S.EmitterSize = 50
		S.MaxDistance = 1500
		S.SoundId = "rbxassetid://".. Explosion[math.random(1, 7)]
		S.PlaybackSpeed = math.random(30,55)/40
		S.Volume = 2
		S.Parent = Exp
		S.PlayOnRemove = true
		S:Destroy()

	--[[for SKP_001, SKP_002 in pairs(game.Players:GetChildren()) do
		if SKP_002:IsA('Player') and SKP_002.Character and SKP_002.Character:FindFirstChild('Head') and (SKP_002.Character.Head.Position - C4.Center.Position).magnitude <= 15 then
			local DistanceMultiplier = (((SKP_002.Character.Head.Position - C4.Center.Position).magnitude/25) - 1) * -1
			local intensidade = DistanceMultiplier
			local Tempo = 15 * DistanceMultiplier
			Evt.Suppression:FireClient(SKP_002,2,intensidade,Tempo)
		end
	end]]	

	Debris:AddItem(BreachPlace,0)

	elseif Mode == 3 then

	Player.Character.Saude.Kit.Fortifications.Value = Player.Character.Saude.Kit.Fortifications.Value - 1
	BreachPlace.Fortified.Value = true
	local C4 = Instance.new('Part')

	C4.Parent = BreachPlace.Destroyable
	C4.Size =  Vector3.new(Hit.Size.X + .05,Hit.Size.Y + .05,Hit.Size.Z + 0.5) 
	C4.Material = Enum.Material.DiamondPlate
	C4.Anchored = true
	C4.CFrame = Hit.CFrame

	local S = Engine.FX.FortFX:Clone()
		S.PlaybackSpeed = math.random(30,55)/40
		S.Volume = 1
		S.Parent = C4
		S.PlayOnRemove = true
		S:Destroy()
	
	end
end)

Evt.Hit.OnServerEvent:Connect(function(Player, Position, HitPart, Normal, Material, Settings)	


	Evt.Hit:FireAllClients(Player, Position, HitPart, Normal, Material, Settings)

	if Settings.ExplosiveHit == true then

		local Hitmark = Instance.new("Attachment")
			Hitmark.CFrame = CFrame.new(Position, Position + Normal)
			Hitmark.Parent = workspace.Terrain
			Debris:AddItem(Hitmark, 5)

		local Exp = Instance.new("Explosion")
		Exp.BlastPressure = Settings.ExPressure
		Exp.BlastRadius = Settings.ExpRadius
		Exp.DestroyJointRadiusPercent = Settings.DestroyJointRadiusPercent
		Exp.Position = Hitmark.Position
		Exp.Parent = Hitmark
		
		local S = Instance.new("Sound")
		S.EmitterSize = 50
		S.MaxDistance = 1500
		S.SoundId = "rbxassetid://".. Explosion[math.random(1, 7)]
		S.PlaybackSpeed = math.random(30,55)/40
		S.Volume = 2
		S.Parent = Exp
		S.PlayOnRemove = true
		S:Destroy()

		Exp.Hit:connect(function(hitPart, partDistance)
  		 local humanoid = hitPart.Parent and hitPart.Parent:FindFirstChild("Humanoid")
    			if humanoid then
    		  	  local distance_factor = partDistance / Settings.ExpRadius    -- get the distance as a value between 0 and 1
      		 		 distance_factor = 1 - distance_factor                         -- flip the amount, so that lower == closer == more damage
					if distance_factor > 0 then
					humanoid:TakeDamage(Settings.ExplosionDamage*distance_factor)                -- 0: no damage; 1: max damage
  					 end
				end
		end)
	end

	--[[for SKP_001, SKP_002 in pairs(game.Players:GetChildren()) do
		if SKP_002:IsA('Player') and SKP_002 ~= Player and SKP_002.Character and SKP_002.Character:FindFirstChild('Head') and (SKP_002.Character.Head.Position - Hitmark.WorldPosition).magnitude <= Settings.SuppressMaxDistance then
			local DistanceMultiplier = (((SKP_002.Character.Head.Position - Hitmark.WorldPosition).magnitude/Settings.SuppressMaxDistance) - 1) * -1
			local intensidade = DistanceMultiplier
			local Tempo = Settings.SuppressTime * DistanceMultiplier
			Evt.Suppression:FireClient(SKP_002,1,intensidade,Tempo)
		end
	end	]]
end)

Evt.LauncherHit.OnServerEvent:Connect(function(Player, Position, HitPart, Normal)
	Evt.LauncherHit:FireAllClients(Player, Position, HitPart, Normal)
end)

Evt.Whizz.OnServerEvent:Connect(function(Player, Vitima)
local Som = Instance.new('Sound')
	Som.Parent = Vitima.PlayerGui
	Som.SoundId = "rbxassetid://"..Whizz[math.random(1,4)]
	Som.Volume = 2
	Som:Play()
	game.Debris:AddItem(Som, Som.TimeLength)
end)


Evt.ServerBullet.OnServerEvent:connect(function(Player, BulletCF, Tracer, Force, BSpeed, Direction, TracerColor,Ray_Ignore,BulletFlare,BulletFlareColor)
	Evt.ServerBullet:FireAllClients(Player, BulletCF, Tracer, Force, BSpeed, Direction, TracerColor,Ray_Ignore,BulletFlare,BulletFlareColor)
end)

Evt.Equipar.OnServerEvent:Connect(function(Player,Arma)

	local Torso = Player.Character:FindFirstChild('Torso')
	local Head = Player.Character:FindFirstChild('Head')
	local HumanoidRootPart = Player.Character:FindFirstChild('HumanoidRootPart')

	if Player.Character:FindFirstChild('Holst' .. Arma.Name) then
		Player.Character['Holst' .. Arma.Name]:Destroy()
	end

	local ServerGun = GunModelServer:FindFirstChild(Arma.Name):clone()
	ServerGun.Name = 'S' .. Arma.Name
	local Settings = require(Arma.ACS_Modulo.Variaveis:WaitForChild("Settings"))

	Arma.ACS_Modulo.Variaveis.BType.Value = Settings.BulletType

	AnimBase = Instance.new("Part", Player.Character)
	AnimBase.FormFactor = "Custom"
	AnimBase.CanCollide = false
	AnimBase.Transparency = 1
	AnimBase.Anchored = false
	AnimBase.Name = "AnimBase"
	AnimBase.Size = Vector3.new(0.1, 0.1, 0.1)
	
	AnimBaseW = Instance.new("Motor6D")
	AnimBaseW.Part0 = AnimBase
	AnimBaseW.Part1 = Head
	AnimBaseW.Parent = AnimBase
	AnimBaseW.Name = "AnimBaseW"

	RA = Player.Character['Right Arm']
	LA = Player.Character['Left Arm']
	RightS = Player.Character.Torso:WaitForChild("Right Shoulder")
	LeftS = Player.Character.Torso:WaitForChild("Left Shoulder")
	
	Right_Weld = Instance.new("Motor6D")
	Right_Weld.Name = "RAW"
	Right_Weld.Part0 = RA
	Right_Weld.Part1 = AnimBase
	Right_Weld.Parent = AnimBase
	Right_Weld.C0 = Settings.RightArmPos
	Player.Character.Torso:WaitForChild("Right Shoulder").Part1 = nil
		
	Left_Weld = Instance.new("Motor6D")
	Left_Weld.Name = "LAW"
	Left_Weld.Part0 = LA
	Left_Weld.Part1 = AnimBase
	Left_Weld.Parent = AnimBase
	Left_Weld.C0 = Settings.LeftArmPos
	Player.Character.Torso:WaitForChild("Left Shoulder").Part1 = nil

	ServerGun.Parent = Player.Character

	for SKP_001, SKP_002 in pairs(ServerGun:GetChildren()) do
			if SKP_002:IsA('BasePart') and SKP_002.Name ~= 'Grip' then
				local SKP_003 = Instance.new('WeldConstraint')
				SKP_003.Parent = SKP_002
				SKP_003.Part0 = SKP_002
				SKP_003.Part1 = ServerGun.Grip
			end;
		end
		
		local SKP_004 = Instance.new('Motor6D')
		SKP_004.Name = 'GripW'
		SKP_004.Parent = ServerGun.Grip
		SKP_004.Part0 = ServerGun.Grip
		SKP_004.Part1 = Player.Character['Right Arm']
		SKP_004.C1 = Settings.ServerGunPos
		
		for L_74_forvar1, L_75_forvar2 in pairs(ServerGun:GetChildren()) do
			if L_75_forvar2:IsA('BasePart') then
				L_75_forvar2.Anchored = false
				L_75_forvar2.CanCollide = false
			end
		end
	
end)

Evt.SilencerEquip.OnServerEvent:Connect(function(Player,Arma,Silencer)
	local Arma = Player.Character['S' .. Arma.Name]
	local Fire

	if Silencer then
		Arma.Silenciador.Transparency = 0
	else
		Arma.Silenciador.Transparency = 1
	end

end)

Evt.Desequipar.OnServerEvent:Connect(function(Player,Arma,Settings)

	if Settings.EnableHolster and Player.Character and Player.Character.Humanoid and Player.Character.Humanoid.Health > 0 then
			if Player.Backpack:FindFirstChild(Arma.Name) then
				local SKP_001 = GunModelServer:FindFirstChild(Arma.Name):clone()
				SKP_001.PrimaryPart = SKP_001.Grip
				SKP_001.Parent = Player.Character
				SKP_001.Name = 'Holst' .. Arma.Name
				
				for SKP_002, SKP_003 in pairs(SKP_001:GetDescendants()) do
					if SKP_003:IsA('BasePart') and SKP_003.Name ~= 'Grip' then
						Weld(SKP_003, SKP_001.Grip)
					end
					
					if SKP_003:IsA('BasePart') and SKP_003.Name == 'Grip' then
						Weld(SKP_003, Player.Character[Settings.HolsterTo], CFrame.new(), Settings.HolsterPos)
					end
				end
				
				for SKP_004, SKP_005 in pairs(SKP_001:GetDescendants()) do
					if SKP_005:IsA('BasePart') then
						SKP_005.Anchored = false
						SKP_005.CanCollide = false
					end
				end
			end
		end

	if Player.Character:FindFirstChild('S' .. Arma.Name) ~= nil then
		Player.Character['S' .. Arma.Name]:Destroy()
		Player.Character.AnimBase:Destroy()
	end

	if Player.Character.Torso:FindFirstChild("Right Shoulder") ~= nil then
		Player.Character.Torso:WaitForChild("Right Shoulder").Part1 = Player.Character['Right Arm']
	end
	if Player.Character.Torso:FindFirstChild("Left Shoulder") ~= nil then
		Player.Character.Torso:WaitForChild("Left Shoulder").Part1 = Player.Character['Left Arm']
	end
	if Player.Character.Torso:FindFirstChild("Neck") ~= nil then
		Player.Character.Torso:WaitForChild("Neck").C0 = CFrame.new(0, 1, 0, -1, -0, -0, 0, 0, 1, 0, 1, 0)
		Player.Character.Torso:WaitForChild("Neck").C1 = CFrame.new(0, -0.5, 0, -1, -0, -0, 0, 0, 1, 0, 1, 0)
	end
end)

Evt.Holster.OnServerEvent:Connect(function(Player,Arma)
	if Player.Character:FindFirstChild('Holst' .. Arma.Name) then
		Player.Character['Holst' .. Arma.Name]:Destroy()
	end
end)

Evt.HeadRot.OnServerEvent:connect(function(Player, Rotacao, Offset, Equipado)
	Evt.HeadRot:FireAllClients(Player, Rotacao, Offset, Equipado)
end)

local TS = game:GetService('TweenService')

Evt.Atirar.OnServerEvent:Connect(function(Player,FireRate,Anims,Arma)
	
	Evt.Atirar:FireAllClients(Player,FireRate,Anims,Arma)

end)

Evt.Stance.OnServerEvent:Connect(function(Player,stance,Settings,Anims)
	if Player.Character.Humanoid.Health > 0 and Player.Character.AnimBase:FindFirstChild("RAW") ~= nil and Player.Character.AnimBase:FindFirstChild("LAW") ~= nil then

		local Right_Weld = Player.Character.AnimBase:WaitForChild("RAW")
		local Left_Weld = Player.Character.AnimBase:WaitForChild("LAW")

		if stance == 0 then
		
			TS:Create(Right_Weld, TweenInfo.new(.3), {C0 = Settings.RightArmPos} ):Play()
			TS:Create(Left_Weld, TweenInfo.new(.3), {C0 = Settings.LeftArmPos} ):Play()

		elseif stance == 2 then
		
			TS:Create(Right_Weld, TweenInfo.new(.3), {C0 = Anims.RightAim} ):Play()
			TS:Create(Left_Weld, TweenInfo.new(.3), {C0 = Anims.LeftAim} ):Play()

		elseif stance == 1 then
		
			TS:Create(Right_Weld, TweenInfo.new(.3), {C0 = Anims.RightHighReady} ):Play()
			TS:Create(Left_Weld, TweenInfo.new(.3), {C0 = Anims.LeftHighReady} ):Play()

		elseif stance == -1 then

			TS:Create(Right_Weld, TweenInfo.new(.3), {C0 = Anims.RightLowReady} ):Play()
			TS:Create(Left_Weld, TweenInfo.new(.3), {C0 = Anims.LeftLowReady} ):Play()

		elseif stance == -2 then

			TS:Create(Right_Weld, TweenInfo.new(.3), {C0 = Anims.RightPatrol} ):Play()
			TS:Create(Left_Weld, TweenInfo.new(.3), {C0 = Anims.LeftPatrol} ):Play()

		elseif stance == 3 then

			TS:Create(Right_Weld, TweenInfo.new(.3), {C0 = Anims.RightSprint} ):Play()
			TS:Create(Left_Weld, TweenInfo.new(.3), {C0 = Anims.LeftSprint} ):Play()
		
		end
	end
end)


Evt.Damage.OnServerEvent:Connect(function(Player,VitimaHuman,Dano,DanoColete,DanoCapacete)
	
	if VitimaHuman ~= nil then
		if VitimaHuman.Parent:FindFirstChild("Saude") ~= nil then
			local Colete = VitimaHuman.Parent.Saude.Protecao.VestVida
			local Capacete = VitimaHuman.Parent.Saude.Protecao.HelmetVida
			Colete.Value = Colete.Value - DanoColete
			Capacete.Value = Capacete.Value - DanoCapacete
		end
	VitimaHuman:TakeDamage(Dano)
	end
	
end)

Evt.CreateOwner.OnServerEvent:Connect(function(Player,VitimaHuman)
	local c = Instance.new("ObjectValue")
			c.Name = "creator"
			c.Value = Player
			game.Debris:AddItem(c, 3)
			c.Parent = VitimaHuman
end)