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



Mouse.Button1Down:connect(function()
	if not UserInputService.TouchEnabled then
	Down = true
	local IsChargedShot = false
	if Equipped and Enabled and Down and not Reloading and not HoldDown and Mag > 0 and Humanoid.Health > 0 then
		Enabled = false
		if Module.ChargedShotEnabled then
			if HandleToFire:FindFirstChild("ChargeSound") then HandleToFire.ChargeSound:Play() end
			wait(Module.ChargingTime)
			IsChargedShot = true
		end
		if Module.MinigunEnabled then
			if HandleToFire:FindFirstChild("WindUp") then HandleToFire.WindUp:Play() end
			wait(Module.DelayBeforeFiring)
		end
		while Equipped and not Reloading and not HoldDown and (Down or IsChargedShot) and Mag > 0 and Humanoid.Health > 0 do
			IsChargedShot = false
			
			Player.PlayerScripts.BulletVisualizerScript.VisualizeM:Fire(nil,HandleToFire,
				                                                       Module.MuzzleFlashEnabled,
			                                                          {Module.MuzzleLightEnabled,Module.LightBrightness,Module.LightColor,Module.LightRange,Module.LightShadows,Module.VisibleTime},
			                                                           script:WaitForChild("MuzzleEffect"))
			VisualizeMuzzle:FireServer(HandleToFire,
				            Module.MuzzleFlashEnabled,
						    {Module.MuzzleLightEnabled,Module.LightBrightness,Module.LightColor,Module.LightRange,Module.LightShadows,Module.VisibleTime},
						    script:WaitForChild("MuzzleEffect"))	
			
			for i = 1,(Module.BurstFireEnabled and Module.BulletPerBurst or 1) do
				spawn(RecoilCamera)
				EjectShell(HandleToFire)
				CrosshairModule.crossspring:accelerate(Module.CrossExpansion)		
				for x = 1,(Module.ShotgunEnabled and Module.BulletPerShot or 1) do
					Fire(HandleToFire, Mouse)
				end
				Mag = Mag - 1
				ChangeMagAndAmmo:FireServer(Mag,Ammo)
				UpdateGUI()
				if Module.BurstFireEnabled then wait(Module.BurstRate) end
				if Mag <= 0 then break end
			end
			HandleToFire = (HandleToFire == Handle and Module.DualEnabled) and Handle2 or Handle
			wait(Module.FireRate)
			if not Module.Auto then break end
		end
		if HandleToFire.FireSound.Playing and HandleToFire.FireSound.Looped then HandleToFire.FireSound:Stop() end
		if Module.MinigunEnabled then
			if HandleToFire:FindFirstChild("WindDown") then HandleToFire.WindDown:Play() end
			wait(Module.DelayAfterFiring)
		end
		Enabled = true
		if Mag <= 0 then Reload() end
	end
	end
end)
Mouse.Button1Up:connect(function()
	if not UserInputService.TouchEnabled then
	    Down = false
	end
end)

ChangeMagAndAmmo.OnClientEvent:connect(function(ChangedMag,ChangedAmmo)
	Mag = ChangedMag
	Ammo = ChangedAmmo
	UpdateGUI()
end)
Tool.Equipped:connect(function(TempMouse)
	GUI.Parent = Player.PlayerGui
	UpdateGUI()
	Handle.EquippedSound:Play()
	if Module.WalkSpeedRedutionEnabled then
	Humanoid.WalkSpeed = Humanoid.WalkSpeed - Module.WalkSpeedRedution
	else
	Humanoid.WalkSpeed = Humanoid.WalkSpeed
	end
	CrosshairModule:setcrosssettings(Module.CrossSize, Module.CrossSpeed, Module.CrossDamper)
	UserInputService.MouseIconEnabled = false
	if EquippedAnim then EquippedAnim:Play(nil,nil,Module.EquippedAnimationSpeed) end
	if IdleAnim then IdleAnim:Play(nil,nil,Module.IdleAnimationSpeed) end
	delay(Module.EquippingTime, function()
	    Equipped = true
	    if Module.AmmoPerMag ~= math.huge then GUI.Frame.Visible = true end
	end)
	TempMouse.KeyDown:connect(function(Key)
		if string.lower(Key) == "r" then
			Reload()
		elseif string.lower(Key) == "e" then
			if not Reloading and not HoldDown and Module.HoldDownEnabled then
				HoldDown = true
				IdleAnim:Stop()
                if HoldDownAnim then HoldDownAnim:Play(nil,nil,Module.HoldDownAnimationSpeed) end
    		    if AimDown then 
                    TweeningService:Create(Camera, TweenInfo.new(Module.TweenLengthNAD, Module.EasingStyleNAD, Module.EasingDirectionNAD), {FieldOfView = 70}):Play()
			        CrosshairModule:setcrossscale(1)
			        --[[local GUI = game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ZoomGui")
			        if GUI then GUI:Destroy() end]]
		        	Scoping = false
			        game:GetService("Players").LocalPlayer.CameraMode = Enum.CameraMode.Classic
			        UserInputService.MouseDeltaSensitivity = InitialSensitivity
			        AimDown = false
	        	end
			else
				HoldDown = false
				IdleAnim:Play(nil,nil,Module.IdleAnimationSpeed)
                if HoldDownAnim then HoldDownAnim:Stop() end
			end
		end
	end)
	Mouse.Button2Down:connect(function()
		if not Reloading and not HoldDown and AimDown == false and Equipped == true and Module.IronsightEnabled and (Character.Head.Position - Camera.CoordinateFrame.p).magnitude <= 1 then
			    TweeningService:Create(Camera, TweenInfo.new(Module.TweenLength, Module.EasingStyle, Module.EasingDirection), {FieldOfView = Module.FieldOfViewIS}):Play()
                CrosshairModule:setcrossscale(Module.CrossScaleIS)
				--[[local GUI = game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ZoomGui") or Tool.ZoomGui:Clone()
				GUI.Parent = game:GetService("Players").LocalPlayer.PlayerGui]]
				--Scoping = false
				game:GetService("Players").LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
				UserInputService.MouseDeltaSensitivity = InitialSensitivity * Module.MouseSensitiveIS
				AimDown = true
		elseif not Reloading and not HoldDown and AimDown == false and Equipped == true and Module.SniperEnabled and (Character.Head.Position - Camera.CoordinateFrame.p).magnitude <= 1 then
			    TweeningService:Create(Camera, TweenInfo.new(Module.TweenLength, Module.EasingStyle, Module.EasingDirection), {FieldOfView = Module.FieldOfViewS}):Play()
				CrosshairModule:setcrossscale(Module.CrossScaleS)
				--[[local GUI = game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ZoomGui") or Tool.ZoomGui:Clone()
				GUI.Parent = game:GetService("Players").LocalPlayer.PlayerGui]]
			    local zoomsound = GUI.Scope.ZoomSound:Clone()
	            zoomsound.Parent = Player.PlayerGui
	            zoomsound:Play()
				Scoping = true
				game:GetService("Players").LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
				UserInputService.MouseDeltaSensitivity = InitialSensitivity * Module.MouseSensitiveS
				AimDown = true
				game:GetService("Debris"):addItem(zoomsound,zoomsound.TimeLength)
			end
	end)
	Mouse.Button2Up:connect(function()
		if AimDown then
            TweeningService:Create(Camera, TweenInfo.new(Module.TweenLengthNAD, Module.EasingStyleNAD, Module.EasingDirectionNAD), {FieldOfView = 70}):Play()
			CrosshairModule:setcrossscale(1)
			--[[local GUI = game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ZoomGui")
			if GUI then GUI:Destroy() end]]
			Scoping = false
			game:GetService("Players").LocalPlayer.CameraMode = Enum.CameraMode.Classic
			UserInputService.MouseDeltaSensitivity = InitialSensitivity
			AimDown = false
		end
	end)
	if Module.DualEnabled and not 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()
	HoldDown = false
	Equipped = false
	GUI.Parent = script
	GUI.Frame.Visible = false
	if Module.WalkSpeedRedutionEnabled then
	Humanoid.WalkSpeed = Humanoid.WalkSpeed + Module.WalkSpeedRedution
	else
	Humanoid.WalkSpeed = Humanoid.WalkSpeed
	end
	UserInputService.MouseIconEnabled = true
	if IdleAnim then IdleAnim:Stop() end
	if HoldDownAnim then HoldDownAnim:Stop() end
		if AimDown then
            TweeningService:Create(Camera, TweenInfo.new(Module.TweenLengthNAD, Module.EasingStyleNAD, Module.EasingDirectionNAD), {FieldOfView = 70}):Play()
			CrosshairModule:setcrossscale(1)
			--[[local GUI = game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ZoomGui")
			if GUI then GUI:Destroy() end]]
			Scoping = false
			game:GetService("Players").LocalPlayer.CameraMode = Enum.CameraMode.Classic
			UserInputService.MouseDeltaSensitivity = InitialSensitivity
			AimDown = false
		end
	if Module.DualEnabled and not workspace.FilteringEnabled then
		Handle2.CanCollide = true
		if Grip2 then Grip2:Destroy() end
	end
end)
Humanoid.Died:connect(function()
	HoldDown = false
	Equipped = false
	GUI.Parent = script
	GUI.Frame.Visible = false
	if Module.WalkSpeedRedutionEnabled then
	Humanoid.WalkSpeed = Humanoid.WalkSpeed + Module.WalkSpeedRedution
	else
	Humanoid.WalkSpeed = Humanoid.WalkSpeed
	end
	UserInputService.MouseIconEnabled = true
	if IdleAnim then IdleAnim:Stop() end
	if HoldDownAnim then HoldDownAnim:Stop() end
		if AimDown then
			TweeningService:Create(Camera, TweenInfo.new(Module.TweenLengthNAD, Module.EasingStyleNAD, Module.EasingDirectionNAD), {FieldOfView = 70}):Play()
			CrosshairModule:setcrossscale(1)
			--[[local GUI = game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ZoomGui")
			if GUI then GUI:Destroy() end]]
			Scoping = false
			game:GetService("Players").LocalPlayer.CameraMode = Enum.CameraMode.Classic
			UserInputService.MouseDeltaSensitivity = InitialSensitivity
			AimDown = false
		end
	if Module.DualEnabled and not workspace.FilteringEnabled then
		Handle2.CanCollide = true
		if Grip2 then Grip2:Destroy() end
	end
end)

local lastTick = tick()

game:GetService("RunService"):BindToRenderStep("Mouse", Enum.RenderPriority.Input.Value, function()
	local deltaTime = tick() - lastTick
	lastTick = tick()
	
	if Scoping and UserInputService.MouseEnabled and UserInputService.KeyboardEnabled then --For pc version
		GUI.Scope.Size = UDim2.new(numLerp(GUI.Scope.Size.X.Scale, 1.2, math.min(deltaTime * 5, 1)), 36, numLerp(GUI.Scope.Size.Y.Scale, 1.2, math.min(deltaTime * 5, 1)), 36)
		GUI.Scope.Position = UDim2.new(0, Mouse.X - GUI.Scope.AbsoluteSize.X / 2, 0, Mouse.Y - GUI.Scope.AbsoluteSize.Y / 2)
	elseif Scoping and UserInputService.TouchEnabled and not UserInputService.MouseEnabled and not UserInputService.KeyboardEnabled then --For mobile version, but in first-person view
		GUI.Scope.Size = UDim2.new(numLerp(GUI.Scope.Size.X.Scale, 1.2, math.min(deltaTime * 5, 1)), 36, numLerp(GUI.Scope.Size.Y.Scale, 1.2, math.min(deltaTime * 5, 1)), 36)
	    GUI.Scope.Position = UDim2.new(0, GUI.Crosshair.AbsolutePosition.X - GUI.Scope.AbsoluteSize.X / 2, 0, GUI.Crosshair.AbsolutePosition.Y - GUI.Scope.AbsoluteSize.Y / 2)
	else
		GUI.Scope.Size = UDim2.new(0.6, 36, 0.6, 36)
		GUI.Scope.Position = UDim2.new(0, 0, 0, 0)
	end
	
	GUI.Scope.Visible = Scoping
	
    if UserInputService.MouseEnabled and UserInputService.KeyboardEnabled then --For pc version
	    GUI.Crosshair.Position = UDim2.new(0, Mouse.X, 0, Mouse.Y)    
    elseif UserInputService.TouchEnabled and not UserInputService.MouseEnabled and not UserInputService.KeyboardEnabled and (Character.Head.Position - Camera.CoordinateFrame.p).magnitude > 2 then --For mobile version, but in third-person view
	    GUI.Crosshair.Position = UDim2.new(0.5, 0, 0.4, -50)
    elseif UserInputService.TouchEnabled and not UserInputService.MouseEnabled and not UserInputService.KeyboardEnabled and (Character.Head.Position - Camera.CoordinateFrame.p).magnitude <= 2 then --For mobile version, but in first-person view
	    GUI.Crosshair.Position = UDim2.new(0.5, -1, 0.5, -19)
    end
	
end)