Player = game.Players.LocalPlayer
Gun = script.Parent
mouse = Player:GetMouse()
BulletDamage = 5
enabled = true
reloading = false
firing = false
canshoot = false
canreload = true

function Shoot()
	if script.Parent.Ammo.Value >= 1 and enabled == true then
		enabled = false
		IsFiring = true
		Firing()
	end
end
function Firing()
	repeat
		
				function Damage(Part)
			if Part.Parent:FindFirstChild("Humanoid") ~= nil and Part.Parent.Name ~= Player.Name then
				Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health - BulletDamage
				Bullet:Destroy()
			end
		end
		script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
		print("You have" .. script.Parent.Ammo.Value .. " ammo left")
		enabled = false
		Bullet = Instance.new("Part", workspace)
		game.Debris:AddItem(Bullet, 2)
		Bullet.Shape = "Ball"
		Bullet.Size = Vector3.new(0.07, 0.07, 0.07)
		Bullet.TopSurface = "Smooth"
		Bullet.BottomSurface = "Smooth"
		Bullet.BrickColor = BrickColor.new("Dark stone grey")
		Bullet.CanCollide = false
		Bullet.CFrame = Gun.Handle.CFrame
		Bullet.CFrame = CFrame.new(Bullet.Position, mouse.Hit.p)
		BM = Instance.new("SpecialMesh", Bullet)
		BM.MeshType = "Sphere"
		BM.Scale = Vector3.new(0.25, 0.25, 0.25)
		v = Instance.new("BodyVelocity", Bullet)
		v.velocity = Bullet.CFrame.lookVector * 90
		v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
		FireSound = Instance.new("Sound", Bullet)
		FireSound.SoundId = "http://www.roblox.com/asset/?id= 78407890"
		RP = math.random(60,80)
		RP = RP/10
		FireSound.Pitch = RP
		RV = math.random(70,90)
		RV = RV/10
		FireSound.Volume = RV
		FireSound:Play()
		game.Debris:AddItem(FireSound, 2)
		Bullet.Touched:connect(Damage)
		wait(0.1)
		enabled = true
		
	until IsFiring == false or script.Parent.Ammo.Value == 0 or reloading == true
end

function StopShooting()
	IsFiring = false
end




--function Reloader()
--	if script.Parent.Ammo.Value < script.Parent.MaxAmmo.Value and  reloading == false and script.Parent.StoredAmmo.Value >= 1 and canreload == true then
--		reloading = true
--		script.Parent.Ammo.Value = 0
--		Reload()
--		if script.Parent.StoredAmmo.Value >=  script.Parent.MaxAmmo.Value then
--			script.Parent.Ammo.Value = script.Parent.MaxAmmo.Value
--			script.Parent.StoredAmmo.Value = script.Parent.StoredAmmo.Value - script.Parent.MaxAmmo.Value
--		elseif script.Parent.StoredAmmo.Value < script.Parent.MaxAmmo.Value and script.Parent.StoredAmmo >= 1 then
--			script.Parent.Ammo.Value = script.Parent.StoredAmmo.Value
--			script.Parent.StoredAmmo.Value = 0
--		end
--		reloading = false
--	end
--end


function Reload(key)
	key = key:lower()
	if enabled == true and script.Parent.Ammo.Value < 30 and key == "r" and script.Parent.Ammo.Value < script.Parent.MaxAmmo.Value and reloading == false and script.Parent.StoredAmmo.Value >= 1 and canreload == true then
		enabled = false
		reloading = true
		script.Parent.Ammo.Value = 0
		RS = Player.Character.Torso["Right Shoulder"]
		for i = 1, 4 do
			RS.C0 = RS.C0 * CFrame.Angles(0,0,0.2)
			wait()
		end
		repeat wait(0.1)
			script.Parent.Ammo.Value = script.Parent.Ammo.Value + 1
			RS.C0 = RS.C0 * CFrame.Angles(0.2,0,0)
			Shell = Instance.new("Part", workspace)
			Shell.TopSurface = "Smooth"
			Shell.BottomSurface = "Smooth"
			Shell.Reflectance = 0.6
			Shell.Size = Vector3.new(1,1,1)
			Shell.CanCollide = false
			ShellMesh = Instance.new("BlockMesh", Shell)
			ShellMesh.Scale = Vector3.new(0.125,0.25,0.125)
			game.Debris:AddItem(Shell, 0.68)
			Shell.CFrame = Gun.Handle.CFrame * CFrame.new(0,-0.5,-0.3)
			wait(0.1)
			RS.C0 = RS.C0 * CFrame.Angles(-0.2,0,0)
		until script.Parent.Ammo.Value == script.Parent.MaxAmmo.Value
		
		for i = 1, 4 do
			RS.C0 = RS.C0 * CFrame.Angles(0,0,-0.2)
			wait()
			if script.Parent.StoredAmmo.Value >= script.Parent.MaxAmmo.Value then
				script.Parent.Ammo.Value = script.Parent.MaxAmmo.Value
				script.Parent.StoredAmmo.Value = script.Parent.StoredAmmo.Value - script.Parent.MaxAmmo.Value
				print("You have " .. script.Parent.StoredAmmo.Value .. " stored ammo left!")
			elseif script.Parent.StoredAmmo.Value < script.Parent.MaxAmmo.Value and script.Parent.StoredAmmo >= 1 then
				script.Parent.Ammo.Value = script.Parent.StoredAmmo.Value
				script.Parent.StoredAmmo.Value = 0
			end
			reloading = false
		end
		wait()
		enabled = true
	end
end
--
--function Reload(key)
--	key = key:lower()
--	if enabled == true and Ammo < 30 and key == "r" then
--		enabled = false
--		RS = Player.Character.Torso["Right Shoulder"]
--		for i = 1, 4 do
--			RS.C0 = RS.C0 * CFrame.Angles(0,0,0.2)
--			wait()
--		end
--		repeat wait(0.1)
--			Ammo = Ammo + 1
--			RS.C0 = RS.C0 * CFrame.Angles(0.2,0,0)
--			Shell = game.Workspace.ClipSize
--			game.Debris:AddItem(Shell, 0.68)
--			Shell.CFrame = Gun.Handle.CFrame * CFrame.new(0,-0.5,-0.3)
--			wait(0.1)
--			RS.C0 = RS.C0 * CFrame.Angles(-0.2,0,0)
--		until Ammo == MaxAmmo
--		
--		for i = 1, 4 do
--			RS.C0 = RS.C0 * CFrame.Angles(0,0,-0.2)
--			wait()
--		end
--		wait()
--		enabled = true
--	end
--end



mouse.KeyDown:connect(Reload)
Gun.Deactivated:connect(StopShooting)
Gun.Activated:connect(Shoot)