--[[local gui = Instance.new("ScreenGui")
local bg = Instance.new("Frame",gui)
local bar = Instance.new("Frame",bg)
local bvl = Instance.new("ImageLabel", bg)
bvl.Name = "Bevel"
bvl.BackgroundTransparency = 1
bvl.Image = "http://www.roblox.com/asset/?id=56852431"
bvl.Size = UDim2.new(1,0,1,0)
bg.Name = "Back"
bar.Name = "Charge"
bar.BackgroundColor3 = Color3.new(200/255,0/255,0/255)
bg.BackgroundColor3 = Color3.new(200/255,200/255,200/255)
bg.Size = UDim2.new(0,10,0,-100)
bg.Position = UDim2.new(0,5,0,500)
bar.Size = UDim2.new(0,4,-1,0)
bar.Position = UDim2.new(0,3,1,0)

ggui = gui:Clone()
ggui.Name = "GunGui"
ggui.Back.Charge.Size = UDim2.new(0,4,-(script.Charge.Value/100),0)]]



GroupID = 9999

function AntiGH(char1,char2)
if GH then
local plyr1 = game.Players:findFirstChild(char1.Name)
local plyr2 = game.Players:findFirstChild(char2.Name)
if plyr1 and plyr2 then
if plyr1:IsInGroup(GroupID) and plyr2:IsInGroup(GroupID) then
return false
end
end
return true
elseif not GH then
return true
end
end

MaxDist = 1000

function RayCast(Start,End,Ignore)
if WallShoot then
ray1 = Ray.new(Start, End.unit * 999.999)
local Part1, TempPos = Workspace:FindPartOnRay(ray1,Ignore)
ray2 = Ray.new(TempPos, End.unit * 999.999)
local Part2, EndPos = Workspace:FindPartOnRay(ray2,Part1)
return Part1, Part2, EndPos
elseif not WallShoot then
ray = Ray.new(Start, End.unit * 999.999)
return Workspace:FindPartOnRay(ray,Ignore)
end
end

function DmgPlr(Part)
if Part ~= nil then
local c = Instance.new("ObjectValue")
c.Name = "creator"
c.Value = game.Players:findFirstChild(script.Parent.Parent.Name)
local hum = Part.Parent:findFirstChild("Humanoid")
local hathum = Part.Parent.Parent:findFirstChild("Humanoid")
local hat = Part.Parent
if hathum ~= nil and hat:IsA("Hat") and AntiGH(hathum.Parent, script.Parent.Parent) then
hathum:TakeDamage(Damage/1)
Part.Parent = game.Workspace
Part.CFrame = CFrame.new(Part.Position + Vector3.new(math.random(-5,5),math.random(-5,5),math.random(-5,5)))
hat:Remove()
c.Parent = hathum
game.Debris:AddItem(c,1.5)
elseif hum ~= nil and AntiGH(hum.Parent, script.Parent.Parent) then
if Part.Name == "Head" then
hum:TakeDamage(Damage*1.3)
end
hum:TakeDamage(Damage)
c.Parent = hum
game.Debris:AddItem(c,1.5)
end
end
end

function onButton1Down(mouse)
if script.Parent.Ammo.Value == 0 then
else
	if GunType == 0 then
		if (not enabled) then return end
		enabled = false
			LaserShoot(mouse)
			if Flash then
			script.Parent.Barrel.Light.Light.Visible = true
			end
			script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
			wait(0.01)
			if Flash then
			script.Parent.Barrel.Light.Light.Visible = false
			end
			wait(1/SPS)
		enabled = true
	elseif GunType == 1 then
		automatichold = true
		while automatichold == true and script.Parent.Ammo.Value ~= 0 do wait()
		if (not enabled) then return end
		if script.Parent.Parent:findFirstChild("Humanoid").Health == 0 then script.Parent:Remove() end
		enabled = false
			LaserShoot(mouse)
			if Flash then
			script.Parent.Barrel.Light.Light.Visible = true
			end
			script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
			wait(0.01)
			if Flash then
			script.Parent.Barrel.Light.Light.Visible = false
			end
			wait(1/SPS)
		enabled = true
		end
	end
end
end

 function LaserShoot(mouse)
	hit = mouse.Hit.p
	local StartPos = script.Parent.Barrel.CFrame.p
	local rv = (StartPos-hit).magnitude/(Recoil * 20)
	local rcl = Vector3.new(math.random(-rv,rv),math.random(-rv,rv),math.random(-rv,rv))
	aim = hit + rcl
	local P = Instance.new("Part") 
	P.Name = "Bullet" 
	P.formFactor = 3
	P.BrickColor = BrickColor.new(BulletColor)
	P.Size = Vector3.new(1,1,1) 
	P.Anchored = true 
	P.CanCollide = false
	P.Transparency = 0.5
	P.Parent = script.Parent.Parent
	local m = Instance.new("CylinderMesh")
	m.Name = "Mesh"
	m.Parent = P
	local c = Instance.new("ObjectValue")
	c.Name = "creator"
	c.Value = game.Players:findFirstChild(script.Parent.Parent.Name)
		pewsound = script:FindFirstChild("Fire")
		if pewsound then 
		pewsound:Play()
		end --Brick created. Moving on to next part
	local SPos = script.Parent.Barrel.CFrame.p
	if WallShoot then
	local Part1, Part2, EndPos = RayCast(SPos, (aim-SPos).unit * 999, script.Parent.Parent)
	DmgPlr(Part1)
	DmgPlr(Part2)
	if Part1 and Part2 then
	local enddist = (EndPos-SPos).magnitude
	P.CFrame = CFrame.new(EndPos, SPos) * CFrame.new(0,0,-enddist/2) * CFrame.Angles(math.rad(90),0,0)
	m.Scale = Vector3.new(.04,enddist,.04)
	else
	P.CFrame = CFrame.new(EndPos, SPos) * CFrame.new(0,0,-MaxDist/2) * CFrame.Angles(math.rad(90),0,0)
	m.Scale = Vector3.new(.04,MaxDist,.04)
	end
	elseif not WallShoot then
	local Part, Pos = RayCast(SPos, (aim-SPos).unit * 999, script.Parent.Parent)
	DmgPlr(Part)
	if Part then
	local dist = (Pos-SPos).magnitude
	P.CFrame = CFrame.new(Pos, SPos) * CFrame.new(0,0,-dist/2) * CFrame.Angles(math.rad(90),0,0)
	m.Scale = Vector3.new(.1,dist,.1)
	else
	P.CFrame = CFrame.new(Pos, SPos) * CFrame.new(0,0,-MaxDist/2) * CFrame.Angles(math.rad(90),0,0)
	m.Scale = Vector3.new(.1,MaxDist,.1)
	end
	end
	game.Debris:AddItem(P,.1)
end

function onButton1Up(mouse)
automatichold = false
end

function onKeyDown(key, mouse)



if key:lower() == "r" then
	if script.Parent.Ammo.Value ~= script.Parent.MaxAmmo.Value then
		reloadsound = script:FindFirstChild("Reload")
		if reloadsound then 
		reloadsound:Play()
		end
		enabled = false
		script.Parent.VisibleB.Value = true
		script.Parent.StringValue.Value = "Reloading"
		repeat script.Parent.StringValue.Value = "Reloading" wait(0.15) script.Parent.Ammo.Value = script.Parent.Ammo.Value + 3 script.Parent.StringValue.Value = "Reloading" until script.Parent.Ammo.Value >= script.Parent.MaxAmmo.Value
		script.Parent.Ammo.Value = script.Parent.MaxAmmo.Value
		wait(0.2)
		script.Parent.VisibleB.Value = false
		enabled = true
	end
end

if key:lower() == "m" then
	if GunType == 1 then
	GunType = 0
	Recoil = 5
	else
	GunType = 1
	Recoil = 4
	end
end

end
		
function onEquipped(mouse)
equipped = true

	if mouse == nil then
		print("Mouse not found")
		return 
	end
	mouse.Icon = "http://www.roblox.com/asset/?id=52812029"
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
	mouse.Button1Up:connect(function() onButton1Up(mouse) end)
	mouse.KeyDown:connect(function(key) onKeyDown(key, mouse) end)

end

function onUnequipped(mouse)
equipped = false
automatichold = false


end

script.Parent.Equipped:connect(onEquipped)
script.Parent.Unequipped:connect(onUnequipped)
while true do wait()
if script.Parent.Ammo.Value == 0 then
		script.Parent.VisibleB.Value = true
		script.Parent.StringValue.Value = "Reload"
end
if GunType == 1 then
script.Parent.ModeText.Value = "Auto"
else
script.Parent.ModeText.Value = "Semi"
end
end