My code will run for a couple of seconds then throw this error: 10:18:55.496 - Workspace.Player.CSG G10.LocalScript:30: attempt to index field 'Parent' (a nil value)
This is the code that is erroring:
local isAuto = true
function ifAuto()
firing = false
if firing == false then
mouse.Button1Down:connect(function() -- I believe that this could be the problem
firing = true
while firing == true do -- while firing
wait(0.5)
script.Parent.Handle.Fire:Play()
local MH = mouse.hit
local B = Instance.new("Part", workspace.Bullet)
game:GetService("Debris"):AddItem(B, 20)
B.CFrame = Gun.Main.CFrame + (char.Torso.CFrame.lookVector * 2)
B.CFrame = CFrame.new(B.Position, MH.p)
B.FormFactor = "Custom"
B.Size = Vector3.new(0.2, 0.2, 0.2)
B.BrickColor = BrickColor.Black()
B.CanCollide = true
local Velocity = Instance.new("BodyVelocity", B)
Velocity.velocity = B.CFrame.lookVector * 120
Velocity.maxForce = Vector3.new(math.huge,math.huge,math.huge)
B.Touched:connect(function(hit) --damage
B:Destroy()
local humanoid = hit.Parent:FindFirstChild("Humanoid") -- error here
if humanoid then
humanoid:TakeDamage(10, 25)
end
end)-- damage function
end --while firing
end)-- mouse down
mouse.Button1Up:connect(function()
firing = false
end)
end
end
This is what calls the function
mouse.Button1Down:connect(function()
local MH = mouse.Hit
if isAuto == true then
ifAuto()
else
createBullet()
script.Parent.Handle.Fire:Play()
end
end)
Thanks for the help

2 comments
You have nothing checking to see if what it hits is a humanoid. Before assigning humanoid, check to see if it exist using conditionals.
I ended up completely redoing the script. It now works almost flawlessly. No errors just a simple bug that is hopefully just as simple to fix...