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

"attempt to index nil with "FindFirstChild""?

I was trying to make some enemy script that shoots you, but I keep stumbling across this problem.

I don't know what's happening but, what I know is that the bullet checks if what they hit has a humanoid in it. If they shoot nowhere, that's when the script will halt.

I have no way how to find every humanoid and how to use it in the bulletHit's argument.

I have lost my insanity especially it's now near midnight so I decided to give up and complain about it instead.

Perhaps I might reverse-engineer the lava brick cus the things these two in common is to damage the player.

--DEFINING--

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local bullet = ReplicatedStorage:WaitForChild("7.62x51mmNATO")

local user = script.Parent
local sound = user.gun.Firesound
local casing = ReplicatedStorage:WaitForChild("shellEject")
local hrp = user.PrimaryPart.Position
local hrpnp = user.PrimaryPart

local gun = user.gun
local br = gun.barrel
local human = nil
local torso = nil

local fireRate = 0.1
local bulletSpeed = 200
local aggroDist = 50
local dmg = 10

print ("succesfully loaded all terms")

--FIRING--

while wait(fireRate) do
	local target = nil
	for i, v in pairs(game.Workspace:GetChildren()) do
		human = v:FindFirstChild("Humanoid")
		torso = v:FindFirstChild("UpperTorso")
		if human and torso and human.Health > 0 then
			if (torso.Position - hrp).magnitude < aggroDist then
				local bulletRay = Ray.new(hrp, (torso.Position - hrp).Unit * aggroDist)
				local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(bulletRay, {script.Parent, script.Parent.Humanoid})
				if hit == torso then
					target = torso
				else
					target = nil
				end
			end
		end
--GUN MECHANICS--		
		if target then
			torso = target
			user:SetPrimaryPartCFrame(CFrame.new(hrp,Vector3.new(script.Parent)))
			hrpnp.CFrame = CFrame.new(hrp, torso.Position)
			local newBullet = bullet:Clone()
			local fire = sound:Clone()
			fire.Parent = gun
			newBullet.Position = hrp
			newBullet.Parent = workspace.Camera
			
			newBullet.Velocity = br.CFrame.LookVector * bulletSpeed
			
			newBullet.Touched:Connect(function(objectHit)
				local human = objectHit.Parent:FindFirstChild("Humanoid")
				if human and human.Parent.Name ~= "hg" then
					human:TakeDamage(dmg)
					wait(1)
					newBullet:destroy()
				else
					human = nil
				end
			end)
		end
	end
end




2 comments

0

What line is the error at?

0

line 56, wheareas the script tries to index/find if there's a humanoid in what they have hit

1 answer

✓ Accepted answer

Hello there.

Problem

You did not use an if statement to check if the objectHit and objectHit's parent wasn't nil.

Solution

Add an if statement to check if the objectHit and objectHit's parent isn't nil.

Fixed Code:

--DEFINING--

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local bullet = ReplicatedStorage:WaitForChild("7.62x51mmNATO")

local user = script.Parent
local sound = user.gun.Firesound
local casing = ReplicatedStorage:WaitForChild("shellEject")
local hrp = user.PrimaryPart.Position
local hrpnp = user.PrimaryPart

local gun = user.gun
local br = gun.barrel
local human = nil
local torso = nil

local fireRate = 0.1
local bulletSpeed = 200
local aggroDist = 50
local dmg = 10

print ("succesfully loaded all terms")

--FIRING--

while wait(fireRate) do
    local target = nil
    for i, v in pairs(game.Workspace:GetChildren()) do
        human = v:FindFirstChild("Humanoid")
        torso = v:FindFirstChild("UpperTorso")
        if human and torso and human.Health > 0 then
            if (torso.Position - hrp).magnitude < aggroDist then
                local bulletRay = Ray.new(hrp, (torso.Position - hrp).Unit * aggroDist)
                local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(bulletRay, {script.Parent, script.Parent.Humanoid})
                if hit == torso then
                    target = torso
                else
                    target = nil
                end
            end
        end
--GUN MECHANICS--       
        if target then
            torso = target
            user:SetPrimaryPartCFrame(CFrame.new(hrp,Vector3.new(script.Parent)))
            hrpnp.CFrame = CFrame.new(hrp, torso.Position)
            local newBullet = bullet:Clone()
            local fire = sound:Clone()
            fire.Parent = gun
            newBullet.Position = hrp
            newBullet.Parent = workspace.Camera

            newBullet.Velocity = br.CFrame.LookVector * bulletSpeed

            newBullet.Touched:Connect(function(objectHit)
		if objectHit and objectHit.Parent then
	                local human = objectHit.Parent:FindFirstChild("Humanoid")
	                if human and human.Parent.Name ~= "hg" then
	                    human:TakeDamage(dmg)
	                    wait(1)
	                    newBullet:destroy()
	                else
	                    human = nil
	                end
		end
            end)
        end
    end
end




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