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 z=Instance.new("\080\097\114\116")z.Name="\101\102\102\101\099\116"z.FormFactor="\067\117\115\116\111\109"z.Size=Vector3.new(1,.4,.33)z.BrickColor=BrickColor.new("\066\114\105\103\104\116 \121\101\108\108\111\119")local L=(WaitForChild(script.Parent,"\066\117\108\108\101\116\077\101\115\104")):Clone()L.Parent=z N.ShellPart=z 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 z=game.Players:GetPlayerFromCharacter(N.Tool.Parent)if k~=nil and z~=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(M,N,k,z,L,l,K,w,s)if M.Ammo.Value<=0 then return end M.Ammo.Value=M.Ammo.Value-1 M:SpawnShell()M.Fire.Pitch=math.random()*.5+.75 M.Fire:Play()M.DoFireAni.Value=not M.DoFireAni.Value print(M.Fire.Pitch)local O=M.Range local j=(N-k).magnitude local X=k+Vector3.new(math.random()-.5,math.random()-.5,math.random()-.5)*(j/100)local b=(X-N).unit local Z=math.ceil(O/L)local I=N for k=1,Z,1 do local z=N+b*(O*(k/Z))local L=(z-I).unit local j=(z-I).magnitude local X,i=CastRay(I,L,j,l,false)DrawBeam(I,i,K,s,M.LaserObj)if X~=nil then local N=X.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if N~=nil then local k=game.Players:GetPlayerFromCharacter(M.Tool.Parent)if k~=nil then local z=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")z.Name="\099\114\101\097\116\111\114"z.Value=k z.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(z)delay(0,function()M:HitEffect(i,X,5)end)else if hit.Name=="\072\101\097\100"then KnockOffHats(hit.Parent)end if GoreOn then delay(0,function()M:HitEffect(i,X,20)end)end if not GLib.IsTeammate(GLib.GetPlayerFromPart(script),GLib.GetPlayerFromPart(N))then GLib.TagHumanoid(GLib.GetPlayerFromPart(script),N,1)N:TakeDamage(w)end end else delay(0,function()M:HitEffect(i,X,5)end)end break end I=i wait(Rate)end if M.Ammo.Value<1 then M:Reload()end end function GunObject.MakeSpark(z,N,k)local L=z.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 l=Instance.new("\066\111\100\121\086\101\108\111\099\105\116\121")l.maxForce=Vector3.new(99999,99999,99999)l.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)))l.Parent=L L.Size=Vector3.new(math.abs(l.velocity.x)/30,math.abs(l.velocity.y)/30,math.abs(l.velocity.z)/30)wait()l:Destroy()local K=.5 game.Debris:AddItem(L,K*2)local w=time()while time()-w<K do if L~=nil then L.Transparency=(time()-w)/K end wait()end if L~=nil then L.Parent=nil end end function GunObject.HitEffect(L,N,k,z)for z=0,z,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 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')
	local 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, bolthit,5) end)
				else
					if(hit.Name=='Head') then
						KnockOffHats(hit.Parent)
					end
					if GoreOn then delay(0,function() self:HitEffect(endpos, bolthit,20) end) end
					if not GLib.IsTeammate(GLib.GetPlayerFromPart(script), GLib.GetPlayerFromPart(h)) 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,p)
	local effect=self.SparkEffect:Clone()
	effect.BrickColor = p.BrickColor
	effect.Material = p.Material
	effect.Transparency = p.Transparency
	effect.Reflectance = p.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,p,numSparks)
	for i = 0, numSparks, 1 do
		Spawn(function() self:MakeSpark(pos,p) end)
	end
	
end
