function GunObject.Initialize(N)N.Fire=WaitForChild(N.Handle,"\070\105\114\101")N.Ammo=N.Tool:FindFirstChild("\065\109\109\111")if N.Ammo~=nil then N.Ammo.Value=N.ClipSize end N.Clips=N.Tool:FindFirstChild("\067\108\105\112\115")if N.Clips~=nil then N.Clips.Value=N.StartingClips end N.Tool.Equipped:connect(function()N.Tool.Handle.Fire:Stop()N.Tool.Handle.Reload:Stop()end)N.Tool.Unequipped:connect(function()N.Tool.Handle.Fire:Stop()N.Tool.Handle.Reload:Stop()end)N.LaserObj=Instance.new("\080\097\114\116")N.LaserObj.Name="\066\117\108\108\101\116"N.LaserObj.Anchored=true N.LaserObj.CanCollide=false N.LaserObj.Shape="\066\108\111\099\107"N.LaserObj.formFactor="\067\117\115\116\111\109"N.LaserObj.Material=Enum.Material.Plastic N.LaserObj.Locked=true N.LaserObj.TopSurface=0 N.LaserObj.BottomSurface=0 local K=Instance.new("\080\097\114\116")K.Name="\069\102\102\101\099\116"K.Anchored=false K.CanCollide=false K.Shape="\066\108\111\099\107"K.formFactor="\067\117\115\116\111\109"K.Material=Enum.Material.Plastic K.Locked=true K.TopSurface=0 K.BottomSurface=0 N.SparkEffect=K local o=Instance.new("\080\097\114\116")o.Name="\101\102\102\101\099\116"o.FormFactor="\067\117\115\116\111\109"o.Size=Vector3.new(1,.4,.33)o.BrickColor=BrickColor.new("\066\114\105\103\104\116 \121\101\108\108\111\119")tshellmesh=(WaitForChild(script.Parent,"\066\117\108\108\101\116\077\101\115\104")):Clone()tshellmesh.Parent=o N.ShellPart=o N.DownVal.Changed:connect(function()while N.DownVal.Value and(N.check and not N.Reloading)do N.check=false local K=N.Tool.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")local o=game.Players:GetPlayerFromCharacter(N.Tool.Parent)if K~=nil and o~=nil then if K.Health>0 then local K=(N.Tool.Handle.CFrame*N.BarrelPos).p delay(0,function()N:SendBullet(K,N.AimVal.Value,N.Spread,N.SegmentLength,N.Tool.Parent,N.Colors[1],N.GunDamage,N.FadeDelayTime)end)else N.check=true break end else N.check=true break end wait(N.FireRate)N.check=true if not N.Automatic then break end end end)N.ReloadingVal.Changed:connect(function()if N.ReloadingVal.Value then N:Reload()end end)end function GunObject.Reload(N)N.Reloading=true N.ReloadingVal.Value=true if N.Clips~=nil then if N.Clips.Value>0 then N.Clips.Value=Clips.Value-1 else N.Reloading=false N.ReloadingVal.Value=false return end end N.Tool.Handle.Reload:Play()for K=1,N.ClipSize,1 do wait(N.ReloadTime/N.ClipSize)N.Ammo.Value=K end N.Reloading=false N.Tool.Reloading.Value=false end function GunObject.SpawnShell(N)local K=N.ShellPart:Clone()K.CFrame=N.Handle.CFrame K.Parent=Workspace game.Debris:AddItem(K,2)end function KnockOffHats(N)for N,K in pairs(N:GetChildren())do if K:IsA("\072\097\116")then K.Parent=game.Workspace end end end function KnockOffTool(N)for N,K in pairs(N:GetChildren())do if K:IsA("\084\111\111\108")then K.Parent=game.Workspace end end end function GunObject.SendBullet(O,N,K,o,l,S,A,X,q)if O.Ammo.Value<=0 then return end O.Ammo.Value=O.Ammo.Value-1 O.Fire.Pitch=math.random()*.5+.75 O.Fire:Play()O.DoFireAni.Value=not O.DoFireAni.Value print(O.Fire.Pitch)local u=O.Range local T=(N-K).magnitude local C=K+Vector3.new(math.random()-.5,math.random()-.5,math.random()-.5)*(T/100)local I=(C-N).unit local y=math.ceil(u/l)local R=N for K=1,y,1 do local o=N+I*(u*(K/y))local l=(o-R).unit local T=(o-R).magnitude local C,v=CastRay(R,l,T,S,false)DrawBeam(R,v,A,q,O.LaserObj)if C~=nil then local N=C.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if N~=nil then local K=game.Players:GetPlayerFromCharacter(O.Tool.Parent)if K~=nil then local o=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")o.Name="\099\114\101\097\116\111\114"o.Value=K o.Parent=N end if hit.Parent:FindFirstChild("\066\108\111\099\107\083\104\111\116")then(hit.Parent:FindFirstChild("\066\108\111\099\107\083\104\111\116")):Fire(o)delay(0,function()O:HitEffect(v,hit,5)end)else local K=hit.Parent:FindFirstChild("\084\111\114\115\111")if hit.Name=="\072\101\097\100"then KnockOffHats(hit.Parent)elseif hit.Name=="\076\101\102\116 \076\101\103"and K:FindFirstChild("\076\101\102\116 \072\105\112")then(K:FindFirstChild("\076\101\102\116 \072\105\112")):Destroy()hit.Parent=Workspace hit.CanCollide=true game.Debris:AddItem(hit,10)elseif hit.Name=="\082\105\103\104\116 \076\101\103"and K:FindFirstChild("\082\105\103\104\116 \072\105\112")then(K:FindFirstChild("\082\105\103\104\116 \072\105\112")):Destroy()hit.Parent=Workspace hit.CanCollide=true game.Debris:AddItem(hit,10)elseif hit.Name=="\076\101\102\116 \065\114\109"and K:FindFirstChild("\076\101\102\116 \083\104\111\117\108\100\101\114")then(K:FindFirstChild("\076\101\102\116 \083\104\111\117\108\100\101\114")):Destroy()hit.Parent=Workspace hit.CanCollide=true game.Debris:AddItem(hit,10)elseif hit.Name=="\082\105\103\104\116 \065\114\109"then KnockOffTool(hit.Parent)end if GLib.IsTeammate(GLib.GetPlayerFromPart(script),GLib.GetPlayerFromPart(N))~=true then GLib.TagHumanoid(GLib.GetPlayerFromPart(script),N,1)N:TakeDamage(X)end end else delay(0,function()O:HitEffect(v,C,5)end)end break end R=v wait(Rate)end if O.Ammo.Value<1 then O:Reload()end end function GunObject.MakeSpark(o,N,K)local l=o.SparkEffect:Clone()l.BrickColor=K.BrickColor l.Material=K.Material l.Transparency=K.Transparency l.Reflectance=K.Reflectance l.CFrame=CFrame.new(N)l.Parent=game.Workspace local S=Instance.new("\066\111\100\121\086\101\108\111\099\105\116\121")S.maxForce=Vector3.new(99999,99999,99999)S.velocity=Vector3.new((math.random()*15)*SigNum(math.random(-10,10)),(math.random()*15)*SigNum(math.random(-10,10)),(math.random()*15)*SigNum(math.random(-10,10)))S.Parent=l l.Size=Vector3.new(math.abs(S.velocity.x)/30,math.abs(S.velocity.y)/30,math.abs(S.velocity.z)/30)wait()S:Destroy()local A=.5 game.Debris:AddItem(l,A*2)local X=time()while time()-X<A do if l~=nil then l.Transparency=(time()-X)/A end wait()end if l~=nil then l.Parent=nil end end function GunObject.HitEffect(l,N,K,o)for o=0,o,1 do spawn(function()l:MakeSpark(N,K)end)end end


function GunObject:Initialize()

	self.Fire=WaitForChild(self.Handle, 'Fire')
	
	self.Ammo = self.Tool:FindFirstChild("Ammo")
	if self.Ammo ~= nil then
		self.Ammo.Value = self.ClipSize
	end
	self.Clips = self.Tool:FindFirstChild("Clips")
	if self.Clips ~= nil then
		self.Clips.Value = self.StartingClips
	end
	self.Tool.Equipped:connect(function()
		self.Tool.Handle.Fire:Stop()
		self.Tool.Handle.Reload:Stop()
	end)
	self.Tool.Unequipped:connect(function()
		self.Tool.Handle.Fire:Stop()
		self.Tool.Handle.Reload:Stop()
	end)
	self.LaserObj = Instance.new("Part")
	self.LaserObj.Name = "Bullet"
	self.LaserObj.Anchored = true
	self.LaserObj.CanCollide = false
	self.LaserObj.Shape = "Block"
	self.LaserObj.formFactor = "Custom"
	self.LaserObj.Material = Enum.Material.Plastic
	self.LaserObj.Locked = true
	self.LaserObj.TopSurface = 0
	self.LaserObj.BottomSurface = 0
	--local tshellmesh=WaitForChild(script.Parent,'BulletMesh'):Clone()
	--tshellmesh.Scale=Vector3.new(4,4,4)
	--tshellmesh.Parent=self.LaserObj

	local tSparkEffect = Instance.new("Part")
	tSparkEffect.Name = "Effect"
	tSparkEffect.Anchored = false
	tSparkEffect.CanCollide = false
	tSparkEffect.Shape = "Block"
	tSparkEffect.formFactor = "Custom"
	tSparkEffect.Material = Enum.Material.Plastic
	tSparkEffect.Locked = true
	tSparkEffect.TopSurface = 0
	tSparkEffect.BottomSurface = 0
	self.SparkEffect=tSparkEffect

	local tshell = Instance.new('Part')
	tshell.Name='effect'
	tshell.FormFactor='Custom'
	tshell.Size=Vector3.new(1, 0.4, 0.33)
	tshell.BrickColor=BrickColor.new('Bright yellow')
	tshellmesh=WaitForChild(script.Parent,'BulletMesh'):Clone()
	tshellmesh.Parent=tshell
	self.ShellPart = tshell

	self.DownVal.Changed:connect(function()
		while self.DownVal.Value and self.check and not self.Reloading do
			self.check = false
			local humanoid = self.Tool.Parent:FindFirstChild("Humanoid")
			local plr1 = game.Players:GetPlayerFromCharacter(self.Tool.Parent)
			if humanoid ~= nil and plr1 ~= nil then
				if humanoid.Health > 0 then
					local spos1 = (self.Tool.Handle.CFrame * self.BarrelPos).p
					delay(0, function() self:SendBullet(spos1, self.AimVal.Value, self.Spread, self.SegmentLength, self.Tool.Parent, self.Colors[1], self.GunDamage, self.FadeDelayTime) end)
				else
					self.check = true
					break
				end
			else
				self.check = true
				break
			end
			wait(self.FireRate)
			self.check = true
			if not self.Automatic then
				break
			end
		end
	end)

	self.ReloadingVal.Changed:connect(function() if self.ReloadingVal.Value then self:Reload() end end)

end



function GunObject:Reload()
	self.Reloading = true
	self.ReloadingVal.Value = true
	if self.Clips ~= nil then
		if self.Clips.Value > 0 then
			self.Clips.Value = Clips.Value - 1
		else
			self.Reloading = false
			self.ReloadingVal.Value = false
			return
		end
	end
	self.Tool.Handle.Reload:Play()
	for i = 1, self.ClipSize do
		wait(self.ReloadTime/self.ClipSize)
		self.Ammo.Value = i
	end
	self.Reloading = false
	self.Tool.Reloading.Value = false
end



function GunObject:SpawnShell()
	local tshell=self.ShellPart:Clone()
	tshell.CFrame=self.Handle.CFrame
	tshell.Parent=Workspace
	game.Debris:AddItem(tshell,2)
end

function KnockOffHats(tchar)
	for _,i in pairs(tchar:GetChildren()) do
		if i:IsA('Hat') then
			i.Parent=game.Workspace
		end
	end
end

function KnockOffTool(tchar)
	for _,i in pairs(tchar:GetChildren()) do
		if i:IsA('Tool') then
			i.Parent=game.Workspace
		end
	end
end

function GunObject:SendBullet(boltstart, targetpos, fuzzyness, SegmentLength, ignore, clr, damage, fadedelay)
	if self.Ammo.Value <=0 then return end
	self.Ammo.Value = self.Ammo.Value - 1
	--self:SpawnShell()
	self.Fire.Pitch = (math.random() * .5) + .75
	self.Fire:Play()
	self.DoFireAni.Value = not self.DoFireAni.Value
	print(self.Fire.Pitch)
	local boltdist = self.Range
	local clickdist = (boltstart - targetpos).magnitude
	local targetpos = targetpos + (Vector3.new(math.random() - .5, math.random() - .5, math.random() - .5) * (clickdist/100))
	local boltvec = (targetpos - boltstart).unit
	local totalsegments = math.ceil(boltdist/SegmentLength)
	local lastpos = boltstart
	for i = 1, totalsegments do
		local newpos = (boltstart + (boltvec * (boltdist * (i/totalsegments))))
		local segvec = (newpos - lastpos).unit
		local boltlength = (newpos - lastpos).magnitude
		local bolthit, endpos = CastRay(lastpos, segvec, boltlength, ignore, false)
		DrawBeam(lastpos, endpos, clr, fadedelay, self.LaserObj)
		if bolthit ~= nil then
			local h = bolthit.Parent:FindFirstChild("Humanoid")
			if h ~= nil then
				local plr = game.Players:GetPlayerFromCharacter(self.Tool.Parent)
				if plr ~= nil then
					local creator = Instance.new("ObjectValue")
					creator.Name = "creator"
					creator.Value = plr
					creator.Parent = h
				end
				if hit.Parent:FindFirstChild("BlockShot") then
						hit.Parent:FindFirstChild("BlockShot"):Fire(newpos)
						delay(0, function() self:HitEffect(endpos, hit,5) end)
				else
					local enemyTorso = hit.Parent:FindFirstChild('Torso')
					if(hit.Name=='Head') then
						KnockOffHats(hit.Parent)
					elseif hit.Name=='Left Leg' and enemyTorso:FindFirstChild('Left Hip') then
						enemyTorso:FindFirstChild('Left Hip'):Destroy()
						hit.Parent=Workspace
						hit.CanCollide=true
						game.Debris:AddItem(hit,10)
					elseif hit.Name=='Right Leg' and enemyTorso:FindFirstChild('Right Hip') then
						enemyTorso:FindFirstChild('Right Hip'):Destroy()
						hit.Parent=Workspace
						hit.CanCollide=true
						game.Debris:AddItem(hit,10)
					elseif hit.Name=='Left Arm' and enemyTorso:FindFirstChild('Left Shoulder') then
						enemyTorso:FindFirstChild('Left Shoulder'):Destroy()
						hit.Parent=Workspace
						hit.CanCollide=true
						game.Debris:AddItem(hit,10)
					elseif hit.Name=='Right Arm' then
						KnockOffTool(hit.Parent)
					end
					if GLib.IsTeammate(GLib.GetPlayerFromPart(script), GLib.GetPlayerFromPart(h))~=true then
						GLib.TagHumanoid(GLib.GetPlayerFromPart(script), h, 1)
						h:TakeDamage(damage)
					end
				end
			else
				delay(0, function() self:HitEffect(endpos, bolthit,5) end)
			end
			break
		end
		lastpos = endpos
		wait(Rate)
	end

	if self.Ammo.Value < 1 then
		self:Reload()
	end

end



function GunObject:MakeSpark(pos,part)
	local effect=self.SparkEffect:Clone()
	effect.BrickColor = part.BrickColor
	effect.Material = part.Material
	effect.Transparency = part.Transparency
	effect.Reflectance = part.Reflectance
	effect.CFrame = CFrame.new(pos)
	effect.Parent = game.Workspace
	local effectVel = Instance.new("BodyVelocity")
	effectVel.maxForce = Vector3.new(99999, 99999, 99999)
	effectVel.velocity = Vector3.new(math.random() * 15 * SigNum(math.random( - 10, 10)), 		math.random() * 15 * SigNum(math.random( - 10, 10)), math.random() * 15 * SigNum(math.random( - 10, 10)))
	effectVel.Parent = effect
	effect.Size = Vector3.new(math.abs(effectVel.velocity.x)/30, math.abs(effectVel.velocity.y)/30, math.abs(effectVel.velocity.z)/30)
	wait()
	effectVel:Destroy()
	local effecttime = .5
	game.Debris:AddItem(effect, effecttime * 2)
	local startTime = time()
	while time() - startTime < effecttime do
		if effect ~= nil then
			effect.Transparency = (time() - startTime)/effecttime
		end
		wait()
	end
	if effect ~= nil then
		effect.Parent = nil
	end
end

function GunObject:HitEffect(pos,part,numSparks)
	for i = 0, numSparks, 1 do
		spawn(function() self:MakeSpark(pos,part) end)
	end
	
end
