Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I fix this? Gun script Works for the first 10 shots but then errors. [closed]

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

0

You have nothing checking to see if what it hits is a humanoid. Before assigning humanoid, check to see if it exist using conditionals.

0

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...

1 answer

For those who want to see the new script:

--player variables
local player = game.Players.LocalPlayer
local char = player.Character
local mouse = player:GetMouse()

--gun variables
local gun = script.Parent -- unused right now
local ammo = 10 -- unused right now
local reloading = false
local auto = true
local firing = false

if not game.workspace:FindFirstChild("Bullet") then
	local bullet_storage = Instance.new("Folder", workspace)
	bullet_storage.Name = "Bullet"
else
	print('Bullet Storage located!')
end

function getBullet()
	local mh = mouse.Hit
	local B = Instance.new("Part", workspace.Bullet)
	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 velo = Instance.new("BodyVelocity", B)
	velo.velocity = B.CFrame.lookVector * 120
	velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
	B.Touched:connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			humanoid:TakeDamage(10, 25)
		end
	end)
end

function autofire()
local debounce = false
	while firing == true do
		wait(0.1)
		if debounce == false then
			debounce = true
			local mh = mouse.Hit
			local B = Instance.new("Part", workspace.Bullet)
			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 velo = Instance.new("BodyVelocity", B)
			velo.velocity = B.CFrame.lookVector * 120
			velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
			B.Touched:connect(function(hit)
				local humanoid = hit.Parent:FindFirstChild("Humanoid")
				if humanoid then
					humanoid:TakeDamage(10, 25)
				end
			wait(0.8)
			debounce = false
			end)
		end
	end
end

mouse.Button1Down:connect(function()
	if auto == true then
		firing = true
		autofire()
	else
		getBullet()
	end
end)

mouse.Button1Up:connect(function()
	if firing == true then
		firing = false
	end
end)

This code runs fine with one exception. When auto is enabled it fires 1 shot then waits a half a second then begins firing fully automatic.

This discussion is preserved as it was. New answers, comments, and votes are closed.