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

Unable to attempt to index nil with 'FindFirstChild' for a 'Humanoid'?

This is a Dagger ServerSide RemoteEvent,

I'm trying to reach the Character Humanoid When the Dagger Reaches the Player As outputed on Line 100 As Unable to Locate the Players Humanoid. When the Dagger Hit/Touches the Player You target. Tried Searching Roblox Wiki Help, Non were very understanding to figure out What Is really My issue.

Output Console

Players.S0NIC_Dev.Backpack.Dagger.DaggerFunction:100: attempt to index nil with 'FindFirstChild'

local gamemodel = workspace.GameModel
local tool = script.Parent
local daggerspeed = script.Parent.Config.daggerspeed.Value
local spinspeed = script.Parent.Config.spinspeed.Value
local maxdistance = script.Parent.Config.maxdistance.Value
local fadetm = script.Parent.Config.fadetm.Value
local character
local torso

script.Parent.Selected:Connect(function()
	character = script.Parent.Parent.Parent.Character
	torso = character.Torso
end)

	local dg = Instance.new("Part")
	dg.formFactor = "Custom"
	dg.Size = Vector3.new(0.2, 0.2, 0.2)
	dg.BrickColor = BrickColor.new("Really black")
	dg.Transparency = 0.2
	dg.Anchored = true
	dg.CanCollide = false
	dg.TopSurface = 0
	dg.BottomSurface = 0
	local m = Instance.new("SpecialMesh")
	m.MeshId = "http://www.roblox.com/asset/?id=20719463"
	m.Scale = Vector3.new(1, 1.4, 1)
	m.Parent = dg
	local loop = Instance.new("Sound")
	loop.SoundId = "http://www.roblox.com/asset/?id=146470466"
	loop.Volume = 1
	loop.Looped = true
	loop.Parent = dg
	
	local dagger_rot = CFrame.Angles(math.rad(-90), math.rad(0), math.rad(-90))
	local spin = CFrame.Angles(0, 0, 0)

function RayCast(ps1, ps2, ignore)
	local ray = Ray.new(
	    ps1,
	    (ps2 - ps1).unit * (ps1 - ps2).magnitude
	)
	local nign = ignore
	nign[#nign + 1] = character
	return workspace:FindPartOnRayWithIgnoreList(ray, nign) --hit, position
end

function GenIgnore()
	local result = {}
	local ch = game.Players:GetPlayers()
	for i = 1, #ch do
		local char = ch[i].Character
		if char ~= nil then
			local sc = char:GetChildren()
			for a = 1, #sc do
				if sc[a]:IsA("Hat") or sc[a]:IsA("Tool") then
					result[#result + 1] = sc[a]
				end
			end
		end
	end
	return result
end

function randomsound()
	local rnd = {"146594648", "146594640", "146457047"}
	return "http://www.roblox.com/asset/?id=" .. rnd[math.random(1, #rnd)]
end

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player,ps)
	local pgui = player.PlayerGui
	local tps = torso.Position
	script.Parent = pgui
	tool.Parent = nil
	--DAGGER SCRIPT
	
	local ignores = GenIgnore()
	ignores[#ignores + 1] = gamemodel
	local tcf = CFrame.new(tps, ps)
	local travelled = 0
	dg.Parent = gamemodel
	dg.CFrame = tcf * dagger_rot * spin
	
	coroutine.resume(coroutine.create(function()
		wait(0.1)
		loop:play()
	end))
	
	while true do
		local st = tick()
		wait()
		local delta = (tick() - st)
		local dist = daggerspeed * delta
		local nps = tcf * CFrame.new(0, 0, -dist)
		spin = spin * CFrame.Angles(0, math.rad(-360 * delta * spinspeed), 0)
		travelled = travelled + dist
		
		--STUFF
		dg.Touched:Connect(function(player,hit,ps)
			local hit, pos = RayCast(tcf.p, nps.p, ignores)
				local hum = hit:FindFirstChild("Humanoid")
				if hum == nil then
					hum = hit.Parent.Parent:FindFirstChild("Humanoid")
				end
				if hum ~= nil then
					ignores[#ignores + 1] = hum.Parent
					local sdf = hum.Parent:FindFirstChild("ShieldDefence")
					if sdf ~= nil then
						sdf.TakeDamage.Value = true
					else
						hum.Parent:BreakJoints()
						local sndp = Instance.new("Part")
						sndp.formFactor = "Custom"
						sndp.Size = Vector3.new(0.2, 0.2, 0.2)
						sndp.Transparency = 1
						sndp.Anchored = true
						sndp.CanCollide = false
						sndp.CFrame = CFrame.new(pos)
						sndp.Parent = gamemodel
						local snd = Instance.new("Sound")
						snd.Volume = 1
						snd.SoundId = randomsound()
						snd.Parent = sndp
						coroutine.resume(coroutine.create(function()
							wait(0.1)
							snd:play()
							wait(1)
							sndp:remove()
						end))
					end
				else
					ignores[#ignores + 1] = hit
				end
			end)
		--STUFF
		
		tcf = nps
		dg.CFrame = tcf * dagger_rot * spin
		if travelled >= maxdistance then
			break
		end
	end
	
	local ld = 0
	while ld ~= 1 do
		local st = tick()
		wait()
		local delta = (tick() - st)
		ld = math.min(1, ld + delta / fadetm)
		local dist = daggerspeed * delta
		dg.Transparency = 0.2 + (0.8 * ld)
		spin = spin * CFrame.Angles(0, math.rad(-360 * delta * spinspeed), 0)
		tcf = tcf * CFrame.new(0, 0, -dist)
		dg.CFrame = tcf * dagger_rot * spin
	end
	
	dg:remove()
	
	--DAGGER SCRIPT
	print("The end")
	script.Parent = nil
end)

2 comments

0

Change hit to hit.Parent

0

I have tried that, And Still get the Unable to Index nil Error.

2 answers

✓ Accepted answer

fix:

local gamemodel = workspace.GameModel
local tool = script.Parent
local daggerspeed = script.Parent.Config.daggerspeed.Value
local spinspeed = script.Parent.Config.spinspeed.Value
local maxdistance = script.Parent.Config.maxdistance.Value
local fadetm = script.Parent.Config.fadetm.Value
local character
local torso

script.Parent.Selected:Connect(function()
    character = script.Parent.Parent.Parent.Character
    torso = character.Torso
end)

    local dg = Instance.new("Part")
    dg.formFactor = "Custom"
    dg.Size = Vector3.new(0.2, 0.2, 0.2)
    dg.BrickColor = BrickColor.new("Really black")
    dg.Transparency = 0.2
    dg.Anchored = true
    dg.CanCollide = false
    dg.TopSurface = 0
    dg.BottomSurface = 0
    local m = Instance.new("SpecialMesh")
    m.MeshId = "http://www.roblox.com/asset/?id=20719463"
    m.Scale = Vector3.new(1, 1.4, 1)
    m.Parent = dg
    local loop = Instance.new("Sound")
    loop.SoundId = "http://www.roblox.com/asset/?id=146470466"
    loop.Volume = 1
    loop.Looped = true
    loop.Parent = dg

    local dagger_rot = CFrame.Angles(math.rad(-90), math.rad(0), math.rad(-90))
    local spin = CFrame.Angles(0, 0, 0)

function RayCast(ps1, ps2, ignore)
    local ray = Ray.new(
        ps1,
        (ps2 - ps1).unit * (ps1 - ps2).magnitude
    )
    local nign = ignore
    nign[#nign + 1] = character
    return workspace:FindPartOnRayWithIgnoreList(ray, nign) --hit, position
end

function GenIgnore()
    local result = {}
    local ch = game.Players:GetPlayers()
    for i = 1, #ch do
        local char = ch[i].Character
        if char ~= nil then
            local sc = char:GetChildren()
            for a = 1, #sc do
                if sc[a]:IsA("Hat") or sc[a]:IsA("Tool") then
                    result[#result + 1] = sc[a]
                end
            end
        end
    end
    return result
end

function randomsound()
    local rnd = {"146594648", "146594640", "146457047"}
    return "http://www.roblox.com/asset/?id=" .. rnd[math.random(1, #rnd)]
end

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player,ps)
    local pgui = player.PlayerGui
    local tps = torso.Position
    script.Parent = pgui
    tool.Parent = nil
    --DAGGER SCRIPT

    local ignores = GenIgnore()
    ignores[#ignores + 1] = gamemodel
    local tcf = CFrame.new(tps, ps)
    local travelled = 0
    dg.Parent = gamemodel
    dg.CFrame = tcf * dagger_rot * spin

    coroutine.resume(coroutine.create(function()
        wait(0.1)
        loop:play()
    end))

    while true do
        local st = tick()
        wait()
        local delta = (tick() - st)
        local dist = daggerspeed * delta
        local nps = tcf * CFrame.new(0, 0, -dist)
        spin = spin * CFrame.Angles(0, math.rad(-360 * delta * spinspeed), 0)
        travelled = travelled + dist

        --STUFF
        dg.Touched:Connect(function(player,hit,ps)
            local hit, pos = RayCast(tcf.p, nps.p, ignores)
                local hum = hit.Parent:FindFirstChild("Humanoid")
                if hum == nil then
                    hum = hit.Parent.Parent:FindFirstChild("Humanoid")
                end
                if hum ~= nil then
                    ignores[#ignores + 1] = hum.Parent
                    local sdf = hum.Parent:FindFirstChild("ShieldDefence")
                    if sdf ~= nil then
                        sdf.TakeDamage.Value = true
                    else
                        hum.Parent:BreakJoints()
                        local sndp = Instance.new("Part")
                        sndp.formFactor = "Custom"
                        sndp.Size = Vector3.new(0.2, 0.2, 0.2)
                        sndp.Transparency = 1
                        sndp.Anchored = true
                        sndp.CanCollide = false
                        sndp.CFrame = CFrame.new(pos)
                        sndp.Parent = gamemodel
                        local snd = Instance.new("Sound")
                        snd.Volume = 1
                        snd.SoundId = randomsound()
                        snd.Parent = sndp
                        coroutine.resume(coroutine.create(function()
                            wait(0.1)
                            snd:play()
                            wait(1)
                            sndp:remove()
                        end))
                    end
                else
                    ignores[#ignores + 1] = hit
                end
            end)
        --STUFF

        tcf = nps
        dg.CFrame = tcf * dagger_rot * spin
        if travelled >= maxdistance then
            break
        end
    end

    local ld = 0
    while ld ~= 1 do
        local st = tick()
        wait()
        local delta = (tick() - st)
        ld = math.min(1, ld + delta / fadetm)
        local dist = daggerspeed * delta
        dg.Transparency = 0.2 + (0.8 * ld)
        spin = spin * CFrame.Angles(0, math.rad(-360 * delta * spinspeed), 0)
        tcf = tcf * CFrame.new(0, 0, -dist)
        dg.CFrame = tcf * dagger_rot * spin
    end

    dg:remove()

    --DAGGER SCRIPT
    print("The end")
    script.Parent = nil
end)

The problem is that raycasts can only get "parts" like right arm and left arm and right lower leg or right leg in r6, so hit can be "right lower arm" and hit.Parent will be the character, what you are doing is searching inside the character parts for a humanoid, you should be using hit.Parent:FindFirstChild("Humanoid") because hit.Parent is the character and you are searching inside the character for the humanoid (which exists), change line 100 to:

local hum = hit.Parent:FindFirstChild("Humanoid")

you should also probably put an if statement to check if hum is not nil so you don't get an error

if hum then
	-- Your code here
end

6 comments

0

This almost worked, But Still only works on Limbs Parts.

0

what parts do you want

0

I am just trying to Reach into the Character It touches, Then Parent to Find the Humanoid Inside the Character.

0

hit.Parent IS the character

0

Still get the Same error "attempt to index nil with 'Parent'."

0

check if hit ~= nil and hit.Parent ~= nil

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