--[[Spawn(function()
	while true do
		fireanimdelay = fireanimdelay - game:GetService("RunService").Heartbeat:wait()
		if fireanimdelay <= 0 then
			fireanimdelay = 0
			if anim2 and fireanimloop then anim2:Stop() fireanimloop = false end
		end
		wait()
	end
end)]]

function wait(TimeToWait)
	if TimeToWait ~= nil then
		local TotalTime = 0
		TotalTime = TotalTime + game:GetService("RunService").Heartbeat:wait()
		while TotalTime < TimeToWait do
			TotalTime = TotalTime + game:GetService("RunService").Heartbeat:wait()
		end
	else
		game:GetService("RunService").Heartbeat:wait()
	end
end

function waitfor(parent,name)
	while parent:FindFirstChild(name)==nil do
		wait()
	end
	return parent:FindFirstChild(name)
end

function checkintangible(hit)
	if hit and hit~=nil then
		if hit:IsDescendantOf(sp.Parent) or hit.Transparency>.8 or hit.Name=="Handle" or hit.Name=="Effect" or hit.Name=="Bullet" or hit.Name=="Laser" or string.lower(hit.Name)=="water" or hit.Name=="Rail" or hit.Name=="Arrow" then
			return true
		end
	end
	return false
end

function castray(startpos,vec,length,ignore,delayifhit)
	local hit,endpos2=game.Workspace:FindPartOnRay(Ray.new(startpos,vec*length),ignore)
	if hit~=nil then
		if checkintangible(hit) then
			if delayifhit then
				wait()
			end
			hit,endpos2=castray(endpos2+(vec*.01),vec,length-((startpos-endpos2).magnitude),ignore,delayifhit)
		end
	end
	return hit,endpos2
end

function drawbeam(beamstart,beamend,clr,fadedelay)
	local dist=(beamstart-beamend).magnitude
	local laser=Instance.new("Part")
	laser.Name="Effect"
	laser.Anchored=true
	laser.CanCollide=false
	laser.Shape="Block"
	laser.formFactor="Custom"
	laser.Size=Vector3.new(.2,.2,.2)
	laser.Transparency=0
	laser.Material=Enum.Material.Plastic
	laser.Locked=true
	laser.TopSurface=0
	laser.BottomSurface=0
	laser.BrickColor=clr
	laser.CFrame=CFrame.new(beamend,beamstart)*CFrame.new(0,0,-dist/2)*CFrame.Angles(math.pi/2,0,0)
	local m=Instance.new("SpecialMesh")
	m.Scale=Vector3.new(1,dist*5,1)
	m.MeshType="Brick"
	m.Parent=laser
	debris:AddItem(laser,fadedelay*0.5)
	laser.Parent=game.Workspace

	--[[local frames=math.floor(fadedelay/rate)
	for frame=1,frames do
		wait(rate)
		local percent=frame/frames
		laser.CFrame=laser.CFrame+windvec*rate
		laser.Transparency=.5+(percent*.5)
	end]]
	wait(.5)
	laser:remove()
end

function fire()
	local hu=sp.Parent:FindFirstChild("Humanoid")
	local he=sp.Parent:FindFirstChild("Head")
	local t=sp.Parent:FindFirstChild("Torso")
	local team=sp.Parent:FindFirstChild("TEAM")
	--if handlestofire == 1 then handlestofire = 2 elseif handlestofire == 2 then handlestofire = 1 end
	if hu and team and hu.Health>0 and t and he and equipped then

		if anim2 and not fireanimloop then anim2:Play() anim2:AdjustSpeed(5) fireanimloop = true end
		fireanimdelay = 0.5
		local startpos=he.Position
		local fakestartpos=(handles[handlestofire].CFrame*CFrame.new(barreloffset)).p
		local vec=(sp.Parent.Target.Value.Position-startpos).unit + (Vector3.new(math.random(-1000,1000),math.random(-1000,1000),math.random(-1000,1000)) / (1500*30))

		local p=Instance.new("Part")
		p.Name="Effect"
		p.BrickColor=BrickColor.new("Black")
		p.CanCollide=false
		p.TopSurface="Smooth"
		p.BottomSurface="Smooth"
		p.formFactor="Custom"
		p.Size=Vector3.new(0,0,0)
		p.Transparency=1
		local m=Instance.new("SpecialMesh")
		m.Parent=p

		local hit,endpos=castray(startpos,vec,range,sp.Parent,false)
		local fakevec=(endpos-fakestartpos).unit
		if hit~=nil then
			local newcf=CFrame.new(endpos,endpos+fakevec)*CFrame.Angles(math.pi/2,0,0)*CFrame.new(0,0,0)
			p.CFrame=newcf
			local w=Instance.new("Weld")
			w.Part0=hit
			w.Part1=p
			w.C0=hit.CFrame:inverse()*newcf
			w.C1=newcf:inverse()*newcf
			w.Parent=p

			--[[local c=Instance.new("ObjectValue")
			c.Name="creator"
			c.Value=game.Players.LocalPlayer
			c.Parent=p]]
			
			team:clone().Parent=p

			local s=script.Script:clone()
			s.Parent=p
			s.Disabled=false
		else
			p.CFrame=CFrame.new(endpos,endpos+fakevec)
			p.Velocity=fakevec*power
			p.Parent=game.Workspace
		end

		debris:AddItem(p,1)
		p.Parent=game.Workspace

		delay(0,function()
			drawbeam(fakestartpos,endpos,BrickColor.new("Bright yellow"),.1)
		end)

		local sound=handles[handlestofire]:FindFirstChild("Fire")
		if sound~=nil then
			--sound:Stop()
			sound:Play()
		end

		local shoulder=t:FindFirstChild("Right Shoulder")
		if shoulder~=nil then
			shoulder.CurrentAngle=(math.pi/2)+.1
		end
	end
end

function onEquipped(mouse)
	equipped=true
	anim = sp.Parent.Humanoid:LoadAnimation(sp.idle)
	if anim then anim:Play() end
	--[[if mouse~=nil then
		themouse=mouse
		mouse.Icon="rbxasset://textures\\GunCursor.png"
		mouse.Button1Down:connect(function()
			mouse.Icon="rbxasset://textures\\GunWaitCursor.png"
			firetime=tick()+spinuptime
			down=true
		end)
		mouse.Button1Up:connect(function()
			down=false
			mouse.Icon="rbxasset://textures\\GunCursor.png"
		end)
	end]]
end

function Reload()
	anim3 = sp.Parent.Humanoid:LoadAnimation(sp.Reload)
	if anim3 then anim3:Play() anim3:AdjustSpeed(2.5) end
	local sound=handles[handlestofire]:FindFirstChild("Reload")
	if sound~=nil then
		sound:Play()
	end
	wait(reloadtime)
	ammo = maxammo
end

function onUnequipped()
	if anim then anim:Stop() end
	equipped=false
	themouse=nil
end

sp.Equipped:connect(onEquipped)
sp.Unequipped:connect(onUnequipped)

while true do
	if sp.Parent.Attack.Value == true then
		if ammo > 0 then
			ammo = ammo - 1
			fire()
			wait(firerate)
		else
			Reload()
		end
	else
		wait()
	end
end