----------------------------------------------------------------------------------------------
--------------------------------[PROGRAMA]----------------------------------------------------
----------------------------------------------------------------------------------------------

	HeadBase = Instance.new('Part')
	HeadBase.Name = 'BasePart'
	HeadBase.Parent = Camera
	HeadBase.Anchored = true
	HeadBase.CanCollide = false
	HeadBase.Transparency = 1
	HeadBase.Size = Vector3.new(0.1, 0.1, 0.1)

	HeadBaseAtt = Instance.new('Attachment')
	HeadBaseAtt.Parent = HeadBase
	


	local StatusUI = PastaHUD:WaitForChild("StatusUI")
	local StatusClone = StatusUI:Clone()
	StatusClone.Parent = Jogador.PlayerGui
	
	if ServerConfig.EnableHunger then
		StatusClone.FomeSede.Disabled = false
	end

if ServerConfig.EnableGPS then
	local StatusUI = PastaHUD:WaitForChild("GPShud")
	local StatusClone = StatusUI:Clone()
	StatusClone.Parent = Jogador.PlayerGui
	StatusClone.GPS.Disabled = false
end

function ResetWorkspace()
	Ignore_Model:ClearAllChildren()
	BulletModel:ClearAllChildren()
	workspace.Terrain:ClearAllChildren()
end

ResetWorkspace()

Evt.Hit.OnClientEvent:Connect(function(Player,Position, HitPart, Normal, Material,Settings)
	if Player ~= Jogador then 
		Hitmarker.HitEffect(Ray_Ignore,ACS_Storage,Position, HitPart, Normal, Material,Settings)
	end
end)

Evt.HeadRot.OnClientEvent:Connect(function(Player, Rotacao, Offset, Equipado)
	if Player ~= Jogador and Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") ~= nil then
		local HRPCF =  Player.Character["HumanoidRootPart"].CFrame * CFrame.new(0, 1.5, 0) * CFrame.new(Offset)
		Player.Character.Torso:WaitForChild("Neck").C0 =  Player.Character.Torso.CFrame:toObjectSpace(HRPCF)
		Player.Character.Torso:WaitForChild("Neck").C1 =  CFrame.Angles(Rotacao, 0, 0)
	end
end)

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

if Player ~= Jogador then 

	if Player.Character:FindFirstChild('S' .. Arma.Name) ~= nil and Player.Character['S' .. Arma.Name].Grip:FindFirstChild("Muzzle") ~= nil then
		local Muzzle = Player.Character['S' .. Arma.Name].Grip:FindFirstChild("Muzzle")
		
		if  Player.Character['S' .. Arma.Name]:FindFirstChild("Silenciador") ~= nil and Player.Character['S' .. Arma.Name].Silenciador.Transparency == 0 then
				Muzzle:FindFirstChild("FlashFX").Brightness = 0
				Muzzle:FindFirstChild("FlashFX[Flash]").Rate = 0
			else
				Muzzle:FindFirstChild("FlashFX").Brightness = 5
				Muzzle:FindFirstChild("FlashFX[Flash]").Rate = 1000
			end


		for _, v in pairs(Muzzle:GetChildren()) do
				if v.Name:sub(1, 7) == "FlashFX" or v.Name:sub(1, 7) == "Smoke" then
					v.Enabled = true
					end
				end

		delay(1 / 30, function()

			for _, v in pairs(Muzzle:GetChildren()) do
				if v.Name:sub(1, 7) == "FlashFX" or v.Name:sub(1, 7) == "Smoke" then
					v.Enabled = false
				end
			end
		end)		

	end
 	if  Player.Character:FindFirstChild("AnimBase") ~= nil and Player.Character.AnimBase:FindFirstChild("AnimBaseW") then
		local AnimBase = Player.Character:WaitForChild("AnimBase"):WaitForChild("AnimBaseW")

		TS:Create(AnimBase, TweenInfo.new(FireRate), {C1 = Anims.ShootPos} ):Play()
		wait(FireRate*2)
		TS:Create(AnimBase, TweenInfo.new(.2), {C1 = CFrame.new()} ):Play()
	end
end
end)

function Setup(Tools)
	
	local Torso = Character:FindFirstChild('Torso')
	local Head = Character:FindFirstChild('Head')
	local HumanoidRootPart = Character:FindFirstChild('HumanoidRootPart')

	ArmaClient = Tools
	ArmaClone = GunModelClient:WaitForChild(ArmaClient.Name):Clone()
	Var = Tools.ACS_Modulo.Variaveis
	Settings = require(Var:WaitForChild("Settings"))
	Anims = require(Var:WaitForChild("Animations"))
	
	 VRecoil = math.random(Settings.VRecoil[1],Settings.VRecoil[2])/1000
	 HRecoil = math.random(Settings.HRecoil[1],Settings.HRecoil[2])/1000
	 VPunchBase = (Settings.VPunchBase)
	 HPunchBase = (Settings.HPunchBase)
	 DPunchBase = (Settings.DPunchBase)
	 RecoilPower = Settings.MinRecoilPower
	 BSpread = Settings.MinSpread

	 Silencer = Var.Suppressor
	 Ammo, StoredAmmo, GLAmmo = Var.Ammo, Var.StoredAmmo, Var.LauncherAmmo
	 Chambered,Emperrado,GLChambered = Var.Chambered, Var.Emperrado, Var.GLChambered
	 FireRate = 1/(Settings.FireRate/60)
	 BurstFireRate = 1/(Settings.BurstFireRate/60)

	 ModoTreino = Settings.ModoTreino

	 Sens = Var.Sens
	 Zeroing = Var.Zeroing


	Evt.Equipar:FireServer(ArmaClient,Settings)

 	Folder = Instance.new("Model", Camera)
	Folder.Name = Tools.Name

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

	Clone = Instance.new('Motor6D')
	Clone.Name = 'Clone'
	Clone.Parent = AnimBase
	Clone.Part0 = AnimBase
	Clone.Part1 = HeadBase


	ArmaClone.Parent = Folder


	for L_209_forvar1, L_210_forvar2 in pairs(ArmaClone:GetChildren()) do
		if L_210_forvar2:IsA('BasePart') and L_210_forvar2.Name ~= 'Handle' then
			if L_210_forvar2.Name ~= "Bolt" and L_210_forvar2.Name ~= 'Lid' then
				Ultil.Weld(L_210_forvar2, ArmaClone:WaitForChild("Handle"))
			end
			
			if L_210_forvar2.Name == "Bolt" or L_210_forvar2.Name == "Slide" then
				Ultil.WeldComplex(ArmaClone:WaitForChild("Handle"), L_210_forvar2, L_210_forvar2.Name)
			end;
			
			if L_210_forvar2.Name == "Lid" then
				if ArmaClone:FindFirstChild('LidHinge') then
					Ultil.Weld(L_210_forvar2, ArmaClone:WaitForChild("LidHinge"))
				else
					Ultil.Weld(L_210_forvar2, ArmaClone:WaitForChild("Handle"))
				end
			end
		end
	end;

	for L_213_forvar1, L_214_forvar2 in pairs(ArmaClone:GetChildren()) do
		if L_214_forvar2:IsA('BasePart') and L_214_forvar2.Name ~= 'Grip' then
			L_214_forvar2.Anchored = false
			L_214_forvar2.CanCollide = false
		end
	end;
	--LoadClientMods()
	RA, LA, Right_Weld, Left_Weld, AnimBase, AnimBaseW = SetupMod(Folder, Ultil, Character, RA, LA, Right_Weld, Left_Weld, AnimBase, AnimBaseW, Settings, ArmaClone)
	Equipped = true
	if ArmaClone:FindFirstChild('Silenciador')~= nil then
		if Silencer.Value == true then
			ArmaClone.Silenciador.Transparency = 0
			ArmaClone.SmokePart.FlashFX.Brightness = 0
			ArmaClone.SmokePart:FindFirstChild("FlashFX[Flash]").Rate = 0
			Evt.SilencerEquip:FireServer(ArmaClient,Silencer.Value)
		else
			ArmaClone.Silenciador.Transparency = 1
			ArmaClone.SmokePart.FlashFX.Brightness = 5
			ArmaClone.SmokePart:FindFirstChild("FlashFX[Flash]").Rate = 1000
			Evt.SilencerEquip:FireServer(ArmaClient,Silencer.Value)
		end
	end
end

function Unset()

	if ArmaClient then
	Evt.Desequipar:FireServer(ArmaClient,Settings)
	end
	UnloadClientMods()
	
	if Folder then
		Folder:Destroy()
	end
	Equipped = false
	Aiming = false
	Safe = false
	Bipod = false
	LanternaAtiva = false
	IRmode = false
	LaserAtivo = false
	--Silencer = false
	CancelReload = false
	Reloading = false
	slideback = false
	OverHeat = false
	uis.MouseIconEnabled = true
	game:GetService('UserInputService').MouseDeltaSensitivity = 1
	Camera.CameraType = Enum.CameraType.Custom
	Player.CameraMode = Enum.CameraMode.Classic
	AimPartMode = 1
	stance = 0
	tweenFoV(70,15)
	TS:Create(StatusClone.Efeitos.Aim,TweenInfo.new(.3),{ImageTransparency = 1}):Play()
	Evt.SVLaser:FireServer(Vector3.new(0,0,0),2,nil,ArmaClient,IRmode)
	if Gui then
		Gui.Visible = false
	end

	for _,c in pairs(Connections) do
			c:disconnect()
		end
		Connections = {}
		Walking = false
		a = false
		d = false
end

function Update_Gui()
	if CanUpdateGui then
				
		if ArmaClone:FindFirstChild("BipodPoint") ~= nil then
			Gui.Bipod.Visible = true
		else
			Gui.Bipod.Visible = false
		end

		if Settings.ArcadeMode == true then
			Gui.Ammo.Visible = true
			Gui.Ammo.AText.Text = Ammo.Value.."|"..Settings.Ammo
		else
			Gui.Ammo.Visible = false
		end

		if Settings.FireModes.Explosive == true and GLChambered.Value == true then
			Gui.E.ImageColor3 = Color3.fromRGB(255,255,255)
			Gui.E.Visible = true
		elseif Settings.FireModes.Explosive == true and GLChambered.Value == false then
			Gui.E.ImageColor3 = Color3.fromRGB(255,0,0)
			Gui.E.Visible = true
		elseif Settings.FireModes.Explosive == false then
			Gui.E.Visible = false
		end

		if Safe == true then
			Gui.A.Visible = true
		else
			Gui.A.Visible = false
		end

		if Chambered.Value == true and Ammo.Value > 0 and Emperrado.Value == false then
			Gui.B.Visible = true
			Gui.B.BackgroundColor3 = Color3.fromRGB(255,255,255)
		elseif Chambered.Value == true and Ammo.Value > 0 and Emperrado.Value == true then
			Gui.B.Visible = true
			Gui.B.BackgroundColor3 = Color3.fromRGB(255,0,0)
		else
			Gui.B.Visible = false
		end
		Gui.FText.Text = Settings.Mode
		
		if Settings.Mode ~= "Explosive" then
			Gui.BText.Text = Settings.BulletType
		else
			Gui.BText.Text = "HEDP"
		end
		Gui.Sens.Text = (Sens.Value/100)
		Gui.ZeText.Text = Zeroing.Value .." m"
		Gui.NText.Text = Settings.Name

		if Settings.Mode ~= "Explosive" then
			if Settings.MagCount then
				Gui.SAText.Text = math.ceil(StoredAmmo.Value/Settings.Ammo)
			else
				Gui.SAText.Text = StoredAmmo.Value
			end
		else
			Gui.SAText.Text =  GLAmmo.Value
		end

		if Silencer.Value == true then
			Gui.Silencer.Visible = true
		else
			Gui.Silencer.Visible = false
		end

		if LaserAtivo == true then
			Gui.Laser.Visible = true
			if IRmode then
				Gui.Laser.ImageColor3 = Color3.new(0,255,0)
			else
				Gui.Laser.ImageColor3 = Color3.new(255,255,255)
			end
		else
			Gui.Laser.Visible = false
		end

		if LanternaAtiva == true then
			Gui.Flash.Visible = true
		else
			Gui.Flash.Visible = false
		end

	end
end

function CheckMagFunction()
	if CanUpdateGui then
		Gui.CMText.TextTransparency = 0
		Gui.CMText.TextStrokeTransparency = 0.9
		if Ammo.Value >= Settings.Ammo then
			Gui.CMText.Text = "Full"
		elseif Ammo.Value > math.floor((Settings.Ammo)*.75) and Ammo.Value < Settings.Ammo then
			Gui.CMText.Text = "Nearly full"
		elseif Ammo.Value < math.floor((Settings.Ammo)*.75) and Ammo.Value > math.floor((Settings.Ammo)*.5) then
			Gui.CMText.Text = "Almost half"
		elseif Ammo.Value == math.floor((Settings.Ammo)*.5) then
			Gui.CMText.Text = "Half"
		elseif Ammo.Value > math.ceil((Settings.Ammo)*.25) and Ammo.Value <  math.floor((Settings.Ammo)*.5) then
			Gui.CMText.Text = "Less than half"
		elseif Ammo.Value < math.ceil((Settings.Ammo)*.25) and Ammo.Value > 0 then
			Gui.CMText.Text = "Almost empty"
		elseif Ammo.Value == 0 then
			Gui.CMText.Text = "Empty"
		end
		TS:Create(Gui.CMText,TweenInfo.new(10),{TextTransparency = 1,TextStrokeTransparency = 1}):Play()
	end
end

function Sprint()
if Equipped then
	if Correndo and SpeedPrecision > 0 then
		MouseHeld = false
		if Aiming then
			game:GetService('UserInputService').MouseDeltaSensitivity = 1
			ArmaClone.Handle.AimUp:Play()	
			tweenFoV(70,120)
			Aiming = false
			TS:Create(StatusClone.Efeitos.Aim,TweenInfo.new(.3),{ImageTransparency = 1}):Play()
		end
		if not Safe and not AnimDebounce then
		stance = 3
		Evt.Stance:FireServer(stance,Settings,Anims)
		SprintAnim()
		end
	elseif not Correndo or SpeedPrecision == 0 then
		if not Safe and not AnimDebounce then
		if Aiming then
		stance = 2
		Evt.Stance:FireServer(stance,Settings,Anims)
		IdleAnim()
		else
		stance = 0
		Evt.Stance:FireServer(stance,Settings,Anims)
		IdleAnim()
		end
		end
	end
end
end

Sprinting.Changed:connect(function(Valor)
	Correndo = Valor
	Sprint()
end)