Tool=script.Parent Handle=Tool:WaitForChild("\072\097\110\100\108\101")Barrel=Handle:WaitForChild("\077\117\122\122\108\101")Players=game:GetService("\080\108\097\121\101\114\115")Debris=game:GetService("\068\101\098\114\105\115")RunService=game:GetService("\082\117\110\083\101\114\118\105\099\101")UserInputService=game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")AmmoDisplay=(script:WaitForChild("\065\109\109\111\068\105\115\112\108\097\121")):Clone()CastLaser=(Tool:WaitForChild("\067\097\115\116\076\097\115\101\114")):Clone()Camera=(game:GetService("\087\111\114\107\115\112\097\099\101")).CurrentCamera BaseUrl="\104\116\116\112\058\047\047\119\119\119\046\114\111\098\108\111\120\046\099\111\109\047\097\115\115\101\116\047\063\105\100\061"AnimationTracks={}LocalObjects={}Animations={Hold={Animation=Tool:WaitForChild("\072\111\108\100"),FadeTime=nil;Weight=nil;Speed=1,Duration=2},Fire={Animation=Tool:WaitForChild("\070\105\114\101");FadeTime=.25;Weight=nil;Speed=.5;Duration=.5},Reload={Animation=Tool:WaitForChild("\082\101\108\111\097\100"),FadeTime=nil,Weight=nil;Speed=.5;Duration=3}}Sounds={Reload=Handle:WaitForChild("\082\101\108\111\097\100"),NoAmmo=Handle:WaitForChild("\078\111\065\109\109\111")}Modules=Tool:WaitForChild("\077\111\100\117\108\101\115")Functions=require(Modules:WaitForChild("\070\117\110\099\116\105\111\110\115"))Remotes=Tool:WaitForChild("\082\101\109\111\116\101\115")ServerControl=Remotes:WaitForChild("\083\101\114\118\101\114\067\111\110\116\114\111\108")ClientControl=Remotes:WaitForChild("\067\108\105\101\110\116\067\111\110\116\114\111\108")ConfigurationBin=Tool:WaitForChild("\067\111\110\102\105\103\117\114\097\116\105\111\110")Configuration={}Configuration=Functions.CreateConfiguration(ConfigurationBin,Configuration)InputCheck=Instance.new("\083\099\114\101\101\110\071\117\105")InputCheck.Name="\073\110\112\117\116\067\104\101\099\107"InputButton=Instance.new("\073\109\097\103\101\066\117\116\116\111\110")InputButton.Name="\073\110\112\117\116\066\117\116\116\111\110"InputButton.Image=""InputButton.BackgroundTransparency=1 InputButton.ImageTransparency=1 InputButton.Size=UDim2.new(1,0,1,0)InputButton.Parent=InputCheck Cursors={Normal=BaseUrl.."\049\055\048\057\048\056\054\054\053",EnemyHit=BaseUrl.."\049\055\050\054\049\056\050\053\057"}Rate=.016666666666667 FiringOffset=Vector3.new(0,Handle.Size.Y/4-.2,-(Handle.Size.Z/2))Reloading=false MouseDown=false ToolEquipped=false Tool.Enabled=true function SetAnimation(x,C)if x=="\080\108\097\121\065\110\105\109\097\116\105\111\110"and(C and(ToolEquipped and Humanoid))then for x,n in pairs(AnimationTracks)do if n.Animation==C.Animation then n.AnimationTrack:Stop()table.remove(AnimationTracks,x)end end local x=Humanoid:LoadAnimation(C.Animation)table.insert(AnimationTracks,{Animation=C.Animation,AnimationTrack=x})x:Play(C.FadeTime,C.Weight,C.Speed)elseif x=="\083\116\111\112\065\110\105\109\097\116\105\111\110"and C then for x,n in pairs(AnimationTracks)do if n.Animation==C.Animation then n.AnimationTrack:Stop(C.FadeTime)table.remove(AnimationTracks,x)end end end end function ToggleGui()if not AmmoDisplayClone or not AmmoDisplayClone.Parent then return end local x=AmmoDisplayClone.Frame local C=x.Ammo if Configuration.Ammo.ClipSize.MaxValue>0 then C.AmmoCounter.CounterPart.Text=Configuration.Ammo.ClipSize.Value end C.MagCounter.CounterPart.Text=Configuration.Ammo.Magazines.Value end function Reload()if Reloading or not Tool.Enabled or Configuration.Ammo.Magazines.Value>=Configuration.Ammo.Magazines.MaxValue then return end Tool.Enabled=false Reloading=true ToggleGui()local x=true if Configuration.Ammo.ClipSize.MaxValue>0 and Configuration.Ammo.ClipSize.Value<=0 then x=false else x=true end if x then Spawn(function()local x=Animations.Reload OnClientInvoke("\080\108\097\121\065\110\105\109\097\116\105\111\110",x)wait(x.Duration)OnClientInvoke("\083\116\111\112\065\110\105\109\097\116\105\111\110",x)end)Sounds.Reload:Play()local x=Configuration.Ammo.Magazines.MaxValue>0 and Configuration.Ammo.Magazines.MaxValue-Configuration.Ammo.Magazines.Value or Configuration.Ammo.ClipSize.MaxValue if Configuration.Ammo.ClipSize.MaxValue>0 then x=x>Configuration.Ammo.ClipSize.Value and Configuration.Ammo.ClipSize.Value or x end wait(Configuration.ReloadTime.Value)Configuration.Ammo.Magazines.Value=Configuration.Ammo.Magazines.Value+x Configuration.Ammo.ClipSize.Value=Configuration.Ammo.ClipSize.Value-x Sounds.Reload:Stop()ToggleGui()end Reloading=false Tool.Enabled=true end function RayTouched(x,C)if not x or not x.Parent then return end local n=x.Parent if n:IsA("\072\097\116")then n=n.Parent end if n==Character then return end local k=n:FindFirstChild("\072\117\109\097\110\111\105\100")local M=Sounds.RayHit if not k or k.Health==0 then return end local T=Players:GetPlayerFromCharacter(n)if T and Functions.IsTeamMate(Player,T)then return end Spawn(function()IconChangeTick=tick()PlayerMouse.Icon=Cursors.EnemyHit wait(1)if tick()-IconChangeTick>=.95 and(ToolEquipped and PlayerMouse)then PlayerMouse.Icon=Cursors.Normal end end)end function FireRay(x,C)local n=(CFrame.new(x,C)).lookVector local k,M,T=Functions.CastRay(x,n,Configuration.Range.Value,{Character},false)local i=Player:FindFirstChild("\066\097\099\107\112\097\099\107")if i then local C=CastLaser:Clone()local n=Instance.new("\086\101\099\116\111\114\051\086\097\108\117\101")n.Name="\083\116\097\114\116\080\111\115\105\116\105\111\110"n.Value=x n.Parent=C local k=Instance.new("\086\101\099\116\111\114\051\086\097\108\117\101")k.Name="\084\097\114\103\101\116\080\111\115\105\116\105\111\110"k.Value=M k.Parent=C local T=Instance.new("\066\111\111\108\086\097\108\117\101")T.Name="\082\097\121\072\105\116"T.Value=T T.Parent=C C.Disabled=false C.Parent=i end Spawn(function()InvokeServer("\067\097\115\116\076\097\115\101\114",{StartPosition=x,TargetPosition=M,RayHit=k and true or false})end)Spawn(function()InvokeServer("\082\097\121\072\105\116",{Hit=k,Position=M})end)RayTouched(k,M)end function Button1Pressed(x)if not x and MouseDown then MouseDown=false end end function KeyPress(x,C)if x=="\114"and C then Reload()end end function Activated()if not Tool.Enabled or not ToolEquipped or Reloading then return end Tool.Enabled=false if Configuration.Ammo.Magazines.Value>0 then local x=false if Configuration.Automatic.Value then MouseDown=true end OnClientInvoke("\083\116\111\112\065\110\105\109\097\116\105\111\110",{Animation=Animations.Fire.Animation,FadeTime=nil})OnClientInvoke("\080\108\097\121\065\110\105\109\097\116\105\111\110",Animations.Fire)while MouseDown or not x and(ToolEquipped and CheckIfAlive())do if Configuration.Ammo.Magazines.Value<=0 or not ToolEquipped or not CheckIfAlive()then break end if not x then x=true end local C=math.random(Configuration.Burst.Bullets.MinValue,Configuration.Burst.Bullets.MaxValue)local n=false Spawn(function()InvokeServer("\070\105\114\101",true)end)for x=1,C>0 and C or 1,1 do local n=OnClientInvoke("\077\111\117\115\101\080\111\115\105\116\105\111\110")if not n then break end n=n.Position local k=Barrel.WorldPosition if C>0 then local x=Configuration.Burst.Offset.Value*100 n=n+Vector3.new(math.random(-x.X,x.X)*.01,math.random(-x.Y,x.Y)*.01,math.random(-x.Z,x.Z)*.01)end local M=Configuration.Accuracy.Value*100 n=n+Vector3.new(math.random(-M.X,M.X)*.01,math.random(-M.Y,M.Y)*.01,math.random(-M.Z,M.Z)*.01)Configuration.Ammo.Magazines.Value=Configuration.Ammo.Magazines.Value-1 FireRay(k,n)end ToggleGui()wait(Configuration.FireRate.Value)end OnClientInvoke("\083\116\111\112\065\110\105\109\097\116\105\111\110",{Animation=Animations.Fire.Animation;FadeTime=.25})else Tool.Enabled=true Sounds.NoAmmo:Play()Reload()end MouseDown=false Tool.Enabled=true if Configuration.Ammo.Magazines.Value<=0 then Sounds.NoAmmo:Play()Reload()end end function CheckIfAlive()return(Player and(Player.Parent and(Character and(Character.Parent and(Humanoid and(Humanoid.Parent and Humanoid.Health>0))))))and true or false end function Equipped(x)Character=Tool.Parent Player=Players:GetPlayerFromCharacter(Character)Humanoid=Character:FindFirstChild("\072\117\109\097\110\111\105\100")if not CheckIfAlive()then return end ToolEquipped=true Spawn(function()PlayerMouse=Player:GetMouse()x.Button1Down:connect(function()Button1Pressed(true)end)x.Button1Up:connect(function()Button1Pressed(false)end)x.KeyDown:connect(function(x)KeyPress(x,true)end)x.KeyUp:connect(function(x)KeyPress(x,false)end)Humanoid.CameraOffset=Vector3.new(0,.35,0)OnClientInvoke("\080\108\097\121\065\110\105\109\097\116\105\111\110",Animations.Hold)local C=Player:FindFirstChild("\080\108\097\121\101\114\071\117\105")if C then if UserInputService.TouchEnabled then InputCheckClone=InputCheck:Clone()InputCheckClone.InputButton.InputBegan:connect(function()InvokeServer("\066\117\116\116\111\110\049\067\108\105\099\107",{Down=true})end)InputCheckClone.InputButton.InputEnded:connect(function()InvokeServer("\066\117\116\116\111\110\049\067\108\105\099\107",{Down=false})end)InputCheckClone.Parent=C end local function x()local x=AmmoDisplayClone.Frame x.CurrentWeapon.Text=Configuration.ToolName.Value local C=x.Ammo C.AmmoCounter.CounterPart.Text=Configuration.Ammo.ClipSize.MaxValue>0 and Configuration.Ammo.ClipSize.Value or"--"C.MagCounter.CounterPart.Text=Configuration.Ammo.Magazines.Value end AmmoDisplayClone=AmmoDisplay:Clone()x()AmmoDisplayClone.Parent=C ToggleGui()for x,C in pairs({ClipSizeChanged;MagazinesChanged})do if C then C:disconnect()end end ClipSizeChanged=Configuration.Ammo.ClipSize.Changed:connect(function()x()end)MagazinesChanged=Configuration.Ammo.Magazines.Changed:connect(function()x()end)end for x,C in pairs({"\076\101\102\116 \065\114\109";"\082\105\103\104\116 \065\114\109"})do local n=Character:FindFirstChild(C)if n then Spawn(function()OnClientInvoke("\083\101\116\076\111\099\097\108\084\114\097\110\115\112\097\114\101\110\099\121\077\111\100\105\102\105\101\114",{Object=n,Transparency=0,AutoUpdate=false})end)end end x.Icon=Cursors.Normal end)end function Unequipped()LocalObjects={}if CheckIfAlive()then Humanoid.CameraOffset=Vector3.new(0,0,0)end for x,C in pairs(Sounds)do C:Stop()end if PlayerMouse then PlayerMouse.Icon=""end for x,C in pairs({InputCheckClone,ObjectLocalTransparencyModifier,AmmoDisplayClone;ClipSizeChanged,MagazinesChanged})do if tostring(C)=="\067\111\110\110\101\099\116\105\111\110"then C:disconnect()elseif C and C.Parent then C:Destroy()end end MouseDown=false for x,C in pairs(AnimationTracks)do if C and C.AnimationTrack then C.AnimationTrack:Stop()end end AnimationTracks={}ToolEquipped=false end function InvokeServer(x,C)pcall(function()local n=ServerControl:InvokeServer(x,C)return n end)end function OnClientInvoke(x,C)if x=="\080\108\097\121\065\110\105\109\097\116\105\111\110"and(C and(ToolEquipped and Humanoid))then SetAnimation("\080\108\097\121\065\110\105\109\097\116\105\111\110",C)elseif x=="\083\116\111\112\065\110\105\109\097\116\105\111\110"and C then SetAnimation("\083\116\111\112\065\110\105\109\097\116\105\111\110",C)elseif x=="\080\108\097\121\083\111\117\110\100"and C then C:Play()elseif x=="\083\116\111\112\083\111\117\110\100"and C then C:Stop()elseif x=="\077\111\117\115\101\080\111\115\105\116\105\111\110"then return{Position=PlayerMouse.Hit.p,Target=PlayerMouse.Target}elseif x=="\083\101\116\076\111\099\097\108\084\114\097\110\115\112\097\114\101\110\099\121\077\111\100\105\102\105\101\114"and(C and ToolEquipped)then pcall(function()local x=false for n,k in pairs(LocalObjects)do if k==C then x=true end end if not x then table.insert(LocalObjects,C)if ObjectLocalTransparencyModifier then ObjectLocalTransparencyModifier:disconnect()end ObjectLocalTransparencyModifier=RunService.RenderStepped:connect(function()for x,C in pairs(LocalObjects)do if C.Object and C.Object.Parent then local x=C.Object.LocalTransparencyModifier if not C.AutoUpdate and(x==1 or x==0)or C.AutoUpdate then C.Object.LocalTransparencyModifier=C.Transparency end else table.remove(LocalObjects,x)end end end)end end)end end ClientControl.OnClientInvoke=OnClientInvoke Tool.Activated:connect(Activated)Tool.Equipped:connect(Equipped)Tool.Unequipped:connect(Unequipped)


Tool = script.Parent
Handle = Tool:WaitForChild("Handle")
Barrel = Handle:WaitForChild("Muzzle")

Players = game:GetService("Players")
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")
UserInputService = game:GetService("UserInputService")

AmmoDisplay = script:WaitForChild("AmmoDisplay"):Clone()
CastLaser = Tool:WaitForChild("CastLaser"):Clone()

Camera = game:GetService("Workspace").CurrentCamera

BaseUrl = "http://www.roblox.com/asset/?id="

AnimationTracks = {}
LocalObjects = {}

Animations = {
	Hold = {Animation = Tool:WaitForChild("Hold"), FadeTime = nil, Weight = nil, Speed = 1, Duration = 2},
	Fire = {Animation = Tool:WaitForChild("Fire"), FadeTime = 0.25, Weight = nil, Speed = 0.5, Duration = 0.5},
	Reload = {Animation = Tool:WaitForChild("Reload"), FadeTime = nil, Weight = nil, Speed = 0.5, Duration = 3},
}

Sounds = {
	Reload = Handle:WaitForChild("Reload"),
	NoAmmo = Handle:WaitForChild("NoAmmo"),
}

Modules = Tool:WaitForChild("Modules")
Functions = require(Modules:WaitForChild("Functions"))

Remotes = Tool:WaitForChild("Remotes")
ServerControl = Remotes:WaitForChild("ServerControl")
ClientControl = Remotes:WaitForChild("ClientControl")

ConfigurationBin = Tool:WaitForChild("Configuration")
Configuration = {}
Configuration = Functions.CreateConfiguration(ConfigurationBin, Configuration)

InputCheck = Instance.new("ScreenGui")
InputCheck.Name = "InputCheck"
InputButton = Instance.new("ImageButton")
InputButton.Name = "InputButton"
InputButton.Image = ""
InputButton.BackgroundTransparency = 1
InputButton.ImageTransparency = 1
InputButton.Size = UDim2.new(1, 0, 1, 0)
InputButton.Parent = InputCheck

Cursors = {
	Normal = (BaseUrl .. "170908665"),
	EnemyHit = (BaseUrl .. "172618259"),
}

Rate = (1 / 60)

FiringOffset = Vector3.new(0, ((Handle.Size.Y / 4) - 0.2), -(Handle.Size.Z / 2))

Reloading = false
MouseDown = false
ToolEquipped = false

Tool.Enabled = true

function SetAnimation(mode, value)
	if mode == "PlayAnimation" and value and ToolEquipped and Humanoid then
		for i, v in pairs(AnimationTracks) do
			if v.Animation == value.Animation then
				v.AnimationTrack:Stop()
				table.remove(AnimationTracks, i)
			end
		end
		local AnimationTrack = Humanoid:LoadAnimation(value.Animation)
		table.insert(AnimationTracks, {Animation = value.Animation, AnimationTrack = AnimationTrack})
		AnimationTrack:Play(value.FadeTime, value.Weight, value.Speed)
	elseif mode == "StopAnimation" and value then
		for i, v in pairs(AnimationTracks) do
			if v.Animation == value.Animation then
				v.AnimationTrack:Stop(value.FadeTime)
				table.remove(AnimationTracks, i)
			end
		end
	end
end

function ToggleGui()
	if not AmmoDisplayClone or not AmmoDisplayClone.Parent then
		return
	end
	local Frame = AmmoDisplayClone.Frame
	local Ammo = Frame.Ammo
	if Configuration.Ammo.ClipSize.MaxValue > 0 then
		Ammo.AmmoCounter.CounterPart.Text = Configuration.Ammo.ClipSize.Value
	end
	Ammo.MagCounter.CounterPart.Text = Configuration.Ammo.Magazines.Value
end

function Reload()
	if Reloading or not Tool.Enabled or Configuration.Ammo.Magazines.Value >= Configuration.Ammo.Magazines.MaxValue then
		return
	end
	Tool.Enabled = false
	Reloading = true
	ToggleGui()
	local CanReload = true
	if Configuration.Ammo.ClipSize.MaxValue > 0 and Configuration.Ammo.ClipSize.Value <= 0 then
		CanReload = false
	else
		CanReload = true
	end
	if CanReload then
		Spawn(function()
			local Animation = Animations.Reload
			OnClientInvoke("PlayAnimation", Animation)
			wait(Animation.Duration)
			OnClientInvoke("StopAnimation", Animation)
		end)
		Sounds.Reload:Play()
		local AddedClips = ((Configuration.Ammo.Magazines.MaxValue > 0 and (Configuration.Ammo.Magazines.MaxValue - Configuration.Ammo.Magazines.Value)) or Configuration.Ammo.ClipSize.MaxValue)
		if Configuration.Ammo.ClipSize.MaxValue > 0 then
			AddedClips = ((AddedClips > Configuration.Ammo.ClipSize.Value and Configuration.Ammo.ClipSize.Value) or AddedClips)
		end
		--[[local ReloadRate = (Configuration.ReloadTime.Value / Configuration.Ammo.Magazines.MaxValue)
		for i = 1, AddedClips do
			wait(ReloadTime)
			Configuration.Ammo.Magazines.Value = (Configuration.Ammo.Magazines.Value + 1)
		end]]
		wait(Configuration.ReloadTime.Value)
		Configuration.Ammo.Magazines.Value = (Configuration.Ammo.Magazines.Value + AddedClips)
		Configuration.Ammo.ClipSize.Value = (Configuration.Ammo.ClipSize.Value - AddedClips)
		Sounds.Reload:Stop()
		ToggleGui()
	end
	Reloading = false
	Tool.Enabled = true
end

function RayTouched(Hit, Position)
	if not Hit or not Hit.Parent then
		return
	end
	local character = Hit.Parent
	if character:IsA("Hat") then
		character = character.Parent
	end
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChild("Humanoid")
	local SoundChosen = Sounds.RayHit
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and Functions.IsTeamMate(Player, player) then
		return
	end
	Spawn(function()
		IconChangeTick = tick()
		PlayerMouse.Icon = Cursors.EnemyHit
		wait(1)
		if (tick() - IconChangeTick) >= 0.95 and ToolEquipped and PlayerMouse then
			PlayerMouse.Icon = Cursors.Normal
		end
	end)
end

function FireRay(StartPosition, TargetPosition)
	local Direction = CFrame.new(StartPosition, TargetPosition).lookVector
	local RayHit, RayPos, RayNormal = Functions.CastRay(StartPosition, Direction, Configuration.Range.Value, {Character}, false)
	local Backpack = Player:FindFirstChild("Backpack")
	if Backpack then
		local LaserScript = CastLaser:Clone()
		local StartPos = Instance.new("Vector3Value")
		StartPos.Name = "StartPosition"
		StartPos.Value = StartPosition
		StartPos.Parent = LaserScript
		local TargetPos = Instance.new("Vector3Value")
		TargetPos.Name = "TargetPosition"
		TargetPos.Value = RayPos
		TargetPos.Parent = LaserScript
		local RayHit = Instance.new("BoolValue")
		RayHit.Name = "RayHit"
		RayHit.Value = RayHit
		RayHit.Parent = LaserScript
		LaserScript.Disabled = false
		LaserScript.Parent = Backpack
	end
	Spawn(function()
		InvokeServer("CastLaser", {StartPosition = StartPosition, TargetPosition = RayPos, RayHit = ((RayHit and true) or false)})
	end)
	Spawn(function()
		InvokeServer("RayHit", {Hit = RayHit, Position = RayPos})
	end)
	RayTouched(RayHit, RayPos)
end

function Button1Pressed(Down)
	if not Down and MouseDown then
		MouseDown = false
	end
end

function KeyPress(Key, Down)
	if Key == "r" and Down then
		Reload()
	end
end

function Activated()
	if not Tool.Enabled or not ToolEquipped or Reloading then
		return
	end
	Tool.Enabled = false
	if Configuration.Ammo.Magazines.Value > 0 then
		local FirstShot = false
		if Configuration.Automatic.Value then
			MouseDown = true
		end
		OnClientInvoke("StopAnimation", {Animation = Animations.Fire.Animation, FadeTime = nil})
		OnClientInvoke("PlayAnimation", Animations.Fire)
		while MouseDown or not FirstShot and ToolEquipped and CheckIfAlive() do
			if Configuration.Ammo.Magazines.Value <= 0 or not ToolEquipped or not CheckIfAlive() then
				break
			end
			if not FirstShot then
				FirstShot = true
			end
			local BurstAmount = math.random(Configuration.Burst.Bullets.MinValue, Configuration.Burst.Bullets.MaxValue)
			local WithinFiringRange = false
			Spawn(function()
				InvokeServer("Fire", true)
			end)
			for i = 1, ((BurstAmount > 0 and BurstAmount) or 1) do
				local TargetPosition = OnClientInvoke("MousePosition")
				if not TargetPosition then
					break
				end
				TargetPosition = TargetPosition.Position
				local StartPosition = Barrel.WorldPosition --(Handle.CFrame * CFrame.new(FiringOffset.X, FiringOffset.Y, FiringOffset.Z)).p
				if BurstAmount > 0 then
					local Offset = (Configuration.Burst.Offset.Value * 100)
					TargetPosition = TargetPosition + Vector3.new((math.random(-Offset.X, Offset.X) * 0.01), (math.random(-Offset.Y, Offset.Y) * 0.01), (math.random(-Offset.Z, Offset.Z) * 0.01))
				end
				local Accuracy = (Configuration.Accuracy.Value * 100)
				TargetPosition = TargetPosition + Vector3.new((math.random(-Accuracy.X, Accuracy.X) * 0.01), (math.random(-Accuracy.Y, Accuracy.Y) * 0.01), (math.random(-Accuracy.Z, Accuracy.Z) * 0.01))
				Configuration.Ammo.Magazines.Value = (Configuration.Ammo.Magazines.Value - 1)
				FireRay(StartPosition, TargetPosition)
			end
			ToggleGui()
			wait(Configuration.FireRate.Value)
		end
		OnClientInvoke("StopAnimation", {Animation = Animations.Fire.Animation, FadeTime = 0.25})
	else
		Tool.Enabled = true
		Sounds.NoAmmo:Play()
		Reload()
	end
	MouseDown = false
	Tool.Enabled = true
	if Configuration.Ammo.Magazines.Value <= 0 then
		Sounds.NoAmmo:Play()
		Reload()
	end
end

function CheckIfAlive()
	return (((Player and Player.Parent and Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0) and true) or false)
end

function Equipped(Mouse)
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChild("Humanoid")
	if not CheckIfAlive() then
		return
	end
	ToolEquipped = true
	Spawn(function()
		PlayerMouse = Player:GetMouse()
		Mouse.Button1Down:connect(function()
			Button1Pressed(true)
		end)
		Mouse.Button1Up:connect(function()
			Button1Pressed(false)
		end)
		Mouse.KeyDown:connect(function(Key)
			KeyPress(Key, true)
		end)
		Mouse.KeyUp:connect(function(Key)
			KeyPress(Key, false)
		end)
		Humanoid.CameraOffset = Vector3.new(0, 0.35, 0)
		OnClientInvoke("PlayAnimation", Animations.Hold)
		local PlayerGui = Player:FindFirstChild("PlayerGui")
		if PlayerGui then
			if UserInputService.TouchEnabled then
				InputCheckClone = InputCheck:Clone()
				InputCheckClone.InputButton.InputBegan:connect(function()
					InvokeServer("Button1Click", {Down = true})
				end)
				InputCheckClone.InputButton.InputEnded:connect(function()
					InvokeServer("Button1Click", {Down = false})
				end)
				InputCheckClone.Parent = PlayerGui
			end
			local function AdjustAmmoDisplay()
				local Frame = AmmoDisplayClone.Frame
				Frame.CurrentWeapon.Text = Configuration.ToolName.Value
				local Ammo = Frame.Ammo
				Ammo.AmmoCounter.CounterPart.Text = ((Configuration.Ammo.ClipSize.MaxValue > 0 and Configuration.Ammo.ClipSize.Value) or "--")
				Ammo.MagCounter.CounterPart.Text = Configuration.Ammo.Magazines.Value
			end
			AmmoDisplayClone = AmmoDisplay:Clone()
			AdjustAmmoDisplay()
			AmmoDisplayClone.Parent = PlayerGui
			ToggleGui()
			for i, v in pairs({ClipSizeChanged, MagazinesChanged}) do
				if v then
					v:disconnect()
				end
			end
			ClipSizeChanged = Configuration.Ammo.ClipSize.Changed:connect(function()
				AdjustAmmoDisplay()
			end)
			MagazinesChanged = Configuration.Ammo.Magazines.Changed:connect(function()
				AdjustAmmoDisplay()
			end)
		end
		for i, v in pairs({"Left Arm", "Right Arm"}) do
			local Arm = Character:FindFirstChild(v)
			if Arm then
				Spawn(function()
					OnClientInvoke("SetLocalTransparencyModifier", {Object = Arm, Transparency = 0, AutoUpdate = false})
				end)
			end
		end
		Mouse.Icon = Cursors.Normal
	end)
end

function Unequipped()
	LocalObjects = {}
	if CheckIfAlive() then
		Humanoid.CameraOffset = Vector3.new(0, 0, 0)
	end
	for i, v in pairs(Sounds) do
		v:Stop()
	end
	if PlayerMouse then
		PlayerMouse.Icon = ""
	end
	for i, v in pairs({InputCheckClone, ObjectLocalTransparencyModifier, AmmoDisplayClone, ClipSizeChanged, MagazinesChanged}) do
		if tostring(v) == "Connection" then
			v:disconnect()
		elseif v and v.Parent then
			v:Destroy()
		end
	end
	MouseDown = false
	for i, v in pairs(AnimationTracks) do
		if v and v.AnimationTrack then
			v.AnimationTrack:Stop()
		end
	end
	AnimationTracks = {}
	ToolEquipped = false
end

function InvokeServer(mode, value)
	pcall(function()
		local ServerReturn = ServerControl:InvokeServer(mode, value)
		return ServerReturn
	end)
end

function OnClientInvoke(mode, value)
	if mode == "PlayAnimation" and value and ToolEquipped and Humanoid then
		SetAnimation("PlayAnimation", value)
	elseif mode == "StopAnimation" and value then
		SetAnimation("StopAnimation", value)
	elseif mode == "PlaySound" and value then
		value:Play()
	elseif mode == "StopSound" and value then
		value:Stop()
	elseif mode == "MousePosition" then
		return {Position = PlayerMouse.Hit.p, Target = PlayerMouse.Target}
	elseif mode == "SetLocalTransparencyModifier" and value and ToolEquipped then
		pcall(function()
			local ObjectFound = false
			for i, v in pairs(LocalObjects) do
				if v == value then
					ObjectFound = true
				end
			end
			if not ObjectFound then
				table.insert(LocalObjects, value)
				if ObjectLocalTransparencyModifier then
					ObjectLocalTransparencyModifier:disconnect()
				end
				ObjectLocalTransparencyModifier = RunService.RenderStepped:connect(function()
					for i, v in pairs(LocalObjects) do
						if v.Object and v.Object.Parent then
							local CurrentTransparency = v.Object.LocalTransparencyModifier
							if ((not v.AutoUpdate and (CurrentTransparency == 1 or  CurrentTransparency == 0)) or v.AutoUpdate) then
								v.Object.LocalTransparencyModifier = v.Transparency
							end
						else
							table.remove(LocalObjects, i)
						end
					end
				end)
			end
		end)
	end
end

ClientControl.OnClientInvoke = OnClientInvoke

Tool.Activated:connect(Activated)
Tool.Equipped:connect(Equipped)
Tool.Unequipped:connect(Unequipped)
