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

Workspace.Zombie.Script:45: attempt to index nil with 'Humanoid'?

function drop() -- Making a drop function
	local maths = math.random(1, 6) -- change 6 to however many nothings and tools you have (Right now its a 1 in 6 chance of dropping the tool)
	local tool = game.ReplicatedStorage.Tools:FindFirstChild("Diamond") -- Finding the tool
	local nothing = game.ReplicatedStorage.Tools:FindFirstChild("Money") -- Finding Nothing

	if maths == 1 then -- The rest of the lines determin which is chosen
		local toolc = tool:Clone()
		toolc.Parent = game.Workspace
		toolc.Handle.CFrame = script.Parent.Head.CFrame + Vector3.new(5,2,0) -- Making the "Tool" Spawn near the Enemy
		script:remove()
	else if maths == 2 then
			local nothingc = nothing:Clone()
			nothingc.Parent = game.Workspace
			nothingc.Handle.CFrame = script.Parent.Head.CFrame + Vector3.new(0,-200,0) -- making the "Nothing" Spawn under the map
			script:remove()
		else if maths == 3 then
				local nothingc = nothing:Clone()
				nothingc.Parent = game.Workspace
				nothingc.Handle.CFrame = script.Parent.Head.CFrame + Vector3.new(0,-200,0)
				script:remove()
			else if maths == 4 then
					local nothingc = nothing:Clone()
					nothingc.Parent = game.Workspace
					nothingc.Handle.CFrame = script.Parent.Head.CFrame + Vector3.new(0,-200,0)
					script:remove()
				else if maths == 5 then
						local nothingc = nothing:Clone()
						nothingc.Parent = game.Workspace
						nothingc.Handle.CFrame = script.Parent.Head.CFrame + Vector3.new(0,-200,0)
						script:remove()
					else if maths == 6 then
							local nothingc = nothing:Clone()
							nothingc.Parent = game.Workspace
							nothingc.Handle.CFrame = script.Parent.Head.CFrame + Vector3.new(0,-200,0)
							script:remove()
						end
					end
				end
			end
		end
	end
end
while true do -- Loop
	wait(.1)
	if script.Parent.Humanoid.Health <= 1 then-- If the Enemy die(ChangeEnemytowhatever 
  the enemys Humanoid is named)
drop() -- calling the drop function
end
end

2 comments

0

is the script in the zombie?

0

Explanation: If something is nil then it doesn't exist, you need to make sure that the humanoid really exists in the given path to the humanoid.

1 answer

try these changes:

function drop() -- Making a drop function
    local maths = math.random(1, 6) -- change 6 to however many nothings and tools you have (Right now its a 1 in 6 chance of dropping the tool)
    local tool = game.ReplicatedStorage.Tools:FindFirstChild("Diamond") -- Finding the tool
    local nothing = game.ReplicatedStorage.Tools:FindFirstChild("Money") -- Finding Nothing
    local Head = script.Parent:FindFirstChild("Head") -- make sure we can find a head! if not then return without breaking the script, but warn saying we have no head!
	if(Head == nil) then warn("Warning: i have no head! \n"..debug.traceback()) return end)
	
    if maths == 1 then -- The rest of the lines determin which is chosen
        local toolc = tool:Clone()
        toolc.Parent = game.Workspace
        toolc.Handle.CFrame = Head.CFrame + Vector3.new(5,2,0) -- Making the "Tool" Spawn near the Enemy (Changed script.Parent.Head to this)
        script:remove()
    elseif maths == 2 then
		local nothingc = nothing:Clone()
		nothingc.Parent = game.Workspace
		nothingc.Handle.CFrame = Head.CFrame + Vector3.new(0,-200,0) -- making the "Nothing" Spawn under the map (Changed script.Parent.Head to this)
		script:remove()
    elseif maths == 3 then
		local nothingc = nothing:Clone()
		nothingc.Parent = game.Workspace
		nothingc.Handle.CFrame = Head.CFrame + Vector3.new(0,-200,0) (Changed script.Parent.Head to this)
		script:remove()
    elseif maths == 4 then
		local nothingc = nothing:Clone()
		nothingc.Parent = game.Workspace
		nothingc.Handle.CFrame = Head.CFrame + Vector3.new(0,-200,0) (Changed script.Parent.Head to this)
		script:remove()
    elseif maths == 5 then
		local nothingc = nothing:Clone()
		nothingc.Parent = game.Workspace
		nothingc.Handle.CFrame = Head.CFrame + Vector3.new(0,-200,0) (Changed script.Parent.Head to this)
		script:remove()
    elseif maths == 6 then
		local nothingc = nothing:Clone()
		nothingc.Parent = game.Workspace
		nothingc.Handle.CFrame = Head.CFrame + Vector3.new(0,-200,0) (Changed script.Parent.Head to this)
		script:remove()
    end
end


-- new little function that searches for the humanoid by class and not by name!
local Humanoid = (function() return script.Parent:FindFirstChildOfClass("Humanoid") end)()


while true do -- Loop
    wait(.1)

    if Humanoid.Health <= 1 then-- If the Enemy die(ChangeEnemytowhatever the enemys Humanoid is named)
		drop() -- calling the drop function
	end
end

Let me know if you have any problems! :)

1 comment

0

Thx

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