--Made by Stickmasterluke



local tool = script.Parent


local firerate = 1
local range = 200


local handle = tool:WaitForChild("Handle")
local event = tool:WaitForChild("RemoteEvent")
local firesound = handle:WaitForChild("FireSound")


local check = true


function checkintangible(hit)
	if hit and hit~=nil then
		if hit:IsDescendantOf(tool.Parent) or hit.Transparency>.8 or hit.Name=="Handle" or string.lower(string.sub(hit.Name,1,6))=="effect" 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
			hit, endpos2 = castray(endpos2+(vec*.01),vec,length-((startpos-endpos2).magnitude),ignore,delayifhit)
		end
	end
	return hit,endpos2
end


tool.Equipped:connect(function(mouse)
	equipped = true
	if mouse then
		mouse.Icon = "rbxasset://textures\\GunCursor.png"
		mouse.Button1Down:connect(function()
			local humanoid = tool.Parent:FindFirstChild("Humanoid")
			local head = tool.Parent:FindFirstChild("Head")
			local torso = tool.Parent:FindFirstChild("Torso")
			if check and humanoid and humanoid.Health > 0 and head and torso then
				check = false
				mouse.Icon = "rbxasset://textures\\GunWaitCursor.png"
				
				firesound:Play()

				local vec = (mouse.Hit.p - head.Position).unit
				local hit ,endpos = castray(head.Position, vec, range, tool.Parent)

				event:FireServer("Fire", endpos, hit)

				wait(firerate)
				if mouse then
					mouse.Icon = "rbxasset://textures\\GunCursor.png"
				end
				check = true
			end
		end)
	end
end)

tool.Unequipped:connect(function()
	equipped = false
end)

