function GunObject.Initialize(U)U.Fire=WaitForChild(U.Handle,"\070\105\114\101")U.Ammo=U.Tool:FindFirstChild("\065\109\109\111")if U.Ammo~=nil then U.Ammo.Value=U.ClipSize end U.Clips=U.Tool:FindFirstChild("\067\108\105\112\115")if U.Clips~=nil then U.Clips.Value=U.StartingClips end U.Tool.Equipped:connect(function()U.Tool.Handle.Fire:Stop()U.Tool.Handle.Reload:Stop()end)U.Tool.Unequipped:connect(function()U.Tool.Handle.Fire:Stop()U.Tool.Handle.Reload:Stop()end)U.LaserObj=Instance.new("\080\097\114\116")U.LaserObj.Name="\066\117\108\108\101\116"U.LaserObj.Anchored=true U.LaserObj.CanCollide=false U.LaserObj.Shape="\066\108\111\099\107"U.LaserObj.formFactor="\067\117\115\116\111\109"U.LaserObj.Material=Enum.Material.Plastic U.LaserObj.Locked=true U.LaserObj.TopSurface=0 U.LaserObj.BottomSurface=0 local j=Instance.new("\080\097\114\116")j.Name="\069\102\102\101\099\116"j.Anchored=false j.CanCollide=false j.Shape="\066\108\111\099\107"j.formFactor="\067\117\115\116\111\109"j.Material=Enum.Material.Plastic j.Locked=true j.TopSurface=0 j.BottomSurface=0 U.SparkEffect=j local H=Instance.new("\080\097\114\116")H.Name="\101\102\102\101\099\116"H.FormFactor="\067\117\115\116\111\109"H.Size=Vector3.new(1,.4,.33)H.BrickColor=BrickColor.new("\066\114\105\103\104\116 \121\101\108\108\111\119")local A=(WaitForChild(script.Parent,"\066\117\108\108\101\116\077\101\115\104")):Clone()A.Parent=H U.ShellPart=H U.DownVal.Changed:connect(function()while U.DownVal.Value and(U.check and not U.Reloading)do U.check=false local j=U.Tool.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")local H=game.Players:GetPlayerFromCharacter(U.Tool.Parent)if j~=nil and H~=nil then if j.Health>0 then local j=(U.Tool.Handle.CFrame*U.BarrelPos).p delay(0,function()U:SendBullet(j,U.AimVal.Value,U.Spread,U.SegmentLength,U.Tool.Parent,U.Colors[1],U.GunDamage,U.FadeDelayTime)end)else U.check=true break end else U.check=true break end wait(U.FireRate)U.check=true if not U.Automatic then break end end end)U.ReloadingVal.Changed:connect(function()if U.ReloadingVal.Value then U:Reload()end end)end function GunObject.Reload(U)U.Reloading=true U.ReloadingVal.Value=true if U.Clips~=nil then if U.Clips.Value>0 then U.Clips.Value=Clips.Value-1 else U.Reloading=false U.ReloadingVal.Value=false return end end U.Tool.Handle.Reload:Play()for j=1,U.ClipSize,1 do wait(U.ReloadTime/U.ClipSize)U.Ammo.Value=j end U.Reloading=false U.Tool.Reloading.Value=false end function GunObject.SpawnShell(U)local j=U.ShellPart:Clone()j.CFrame=U.Handle.CFrame j.Parent=Workspace game.Debris:AddItem(j,2)end function KnockOffHats(U)for U,j in pairs(U:GetChildren())do if j:IsA("\072\097\116")then j.Parent=game.Workspace end end end function KnockOffTool(U)for U,j in pairs(U:GetChildren())do if j:IsA("\084\111\111\108")then j.Parent=game.Workspace end end end function GunObject.SendBullet(G,U,j,H,A,Q,u,Y,z)if G.Ammo.Value<=0 then return end G.Ammo.Value=G.Ammo.Value-1 G:SpawnShell()G.Fire.Pitch=math.random()*.5+.75 G.Fire:Play()G.DoFireAni.Value=not G.DoFireAni.Value print(G.Fire.Pitch)local c=G.Range local g=(U-j).magnitude local o=j+(Vector3.new(math.random()-.5,math.random()-.5,math.random()-.5)*(g/100))*H local D=(o-U).unit local x=math.ceil(c/A)local p=U for j=1,x,1 do local H=U+D*(c*(j/x))local A=(H-p).unit local g=(H-p).magnitude local o,E=CastRay(p,A,g,Q,false)DrawBeam(p,E,u,z,G.LaserObj)if o~=nil then local U=o.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if U~=nil then local j=game.Players:GetPlayerFromCharacter(G.Tool.Parent)if j~=nil then local H=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")H.Name="\099\114\101\097\116\111\114"H.Value=j H.Parent=U 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(H)delay(0,function()G:HitEffect(E,BrickColor.new("\077\101\100\105\117\109 \115\116\111\110\101 \103\114\101\121"),5)end)else if hit.Name=="\072\101\097\100"then KnockOffHats(hit.Parent)end if GoreOn then delay(0,function()G:HitEffect(E,BrickColor.new("\066\114\105\103\104\116 \114\101\100"),20)end)end U:TakeDamage(Y)end else delay(0,function()G:HitEffect(E,BrickColor.new("\077\101\100\105\117\109 \115\116\111\110\101 \103\114\101\121"),5)end)end break end p=E wait(Rate)end if G.Ammo.Value<1 then G:Reload()end end function GunObject.MakeSpark(H,U,j)local A=H.SparkEffect:Clone()A.BrickColor=j A.CFrame=CFrame.new(U)A.Parent=game.Workspace local Q=Instance.new("\066\111\100\121\086\101\108\111\099\105\116\121")Q.maxForce=Vector3.new(99999,99999,99999)Q.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)))Q.Parent=A A.Size=Vector3.new(math.abs(Q.velocity.x)/30,math.abs(Q.velocity.y)/30,math.abs(Q.velocity.z)/30)wait()Q:Destroy()local u=.5 game.Debris:AddItem(A,u*2)local Y=time()while time()-Y<u do if A~=nil then A.Transparency=(time()-Y)/u end wait()end if A~=nil then A.Parent=nil end end function GunObject.HitEffect(A,U,j,H)for H=0,H,1 do Spawn(function()A:MakeSpark(U,j)end)end end
--[[Member functions]]

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() * 0.5) + 0.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() - 0.5, math.random() - 0.5, math.random() - 0.5) * (clickdist / 100) * fuzzyness)
	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, BrickColor.new("Medium stone grey"), 5)
					end)
				else
					if hit.Name == "Head" then
						KnockOffHats(hit.Parent)
					end
					if GoreOn then
						delay(0, function()
							self:HitEffect(endpos, BrickColor.new("Bright red"), 20)
						end)
					end
					h:TakeDamage(damage)
				end
			else
				delay(0, function()
					self:HitEffect(endpos, BrickColor.new("Medium stone grey"), 5)
				end)
			end
			break
		end
		lastpos = endpos
		wait(Rate)
	end

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

function GunObject:MakeSpark(pos, tcolor)
	local effect = self.SparkEffect:Clone()
	effect.BrickColor = tcolor
	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 = 0.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, tcolor, numSparks)
	for i = 0, numSparks, 1 do
		Spawn(function()
			self:MakeSpark(pos, tcolor)
		end)
	end
end
