Drooling Zombie (Rthro) Error HumanoidRootPart is not a valid member of Workspace "Workspace"

  1. What do you want to achieve? Keep it simple and clear!

Not spit out errors when the player is not in line of sight of zombie

  1. What is the issue? Include screenshots / videos if possible!

image

HumanoidRootPart is not a valid member of Workspace “Workspace” when zombie not in line of sight

So im trying to use the roblox’s “Drooling Zombie (Rthro)” model but it just spits outs errors and the zombie doesn’t try to attack

image

local ray = Ray.new(
			maid.humanoidRootPart.Position,
			(targetInstance.Parent.HumanoidRootPart.Position - maid.humanoidRootPart.Position).Unit * distance
		)

Use FindFirstChild instead, this will work.

if targetInstance.Parent:FindFirstChild("HumanoidRootPart") then
   ... -- run the code
end

image

This spits outs a different error when not in line of sight

local ray = Ray.new(
			maid.humanoidRootPart.Position,
			(targetInstance.Parent:FindFirstChild("HumanoidRootPart").Position - maid.humanoidRootPart.Position).Unit * distance
		)

As I said you have to verify if it;s valid or not, with the code I posted above.

No, this is incorrect you need to have an if statement before then and check if the HumanoidRootPart is valid via FindFirstChild, you dont have to change the raycast code much, just move all the code into the if statement

if targetInstance.Parent:FindFirstChild("HumanoidRootPart") then
   ... -- Raycast code and everything else here.
end

can you show us where the maid variable is declared?

Like this?

if targetInstance.Parent:FindFirstChild("HumanoidRootPart") then
		 
	if distance <= ATTACK_RADIUS then -- run the code
		local ray = Ray.new(
			maid.humanoidRootPart.Position,
			(targetInstance.ParentHumanoidRootPart.Position - maid.humanoidRootPart.Position).Unit * distance
		)

		local part = Workspace:FindPartOnRayWithIgnoreList(ray, {
			targetInstance.Parent, maid.instance,
		}, false, true)

		if
			targetInstance ~= maid.instance and
			targetInstance:IsDescendantOf(Workspace) and
			targetHumanoid.Health > 0 and
			targetHumanoid:GetState() ~= Enum.HumanoidStateType.Dead and
			not CollectionService:HasTag(targetInstance.Parent, "ZombieFriend") and
			not part
		then
			isAttackable = true
		end
	end
end

when running this it outputs

script

image
maid is a module script

local Maid = {}

function Maid.new()
	local self = {
		_tasks = {},
	}
	setmetatable(self, Maid)

	return self
end

function Maid:__index(key)
	return Maid[key] or self._tasks[key]
end

function Maid:__newindex(key, newTask)
	if Maid[key] then
		error(string.format("Cannot use %q as a Maid key", tostring(key)))
	end
	local tasks = self._tasks
	local oldTask = tasks[key]
	tasks[key] = newTask

	if oldTask then
		Maid.cleanupTask(oldTask)
	end
end

function Maid:give(task)
	local tasks = self._tasks
	tasks[#tasks+1] = task
end

function Maid.cleanupTask(task)
	local taskTy = typeof(task)
	if taskTy == 'function' then
		task()
	elseif taskTy == 'RBXScriptConnection' then
		task:Disconnect()
	elseif taskTy == 'Instance' then
		task:Destroy()
	elseif task.Destroy then
		task:Destroy()
	elseif task.destroy then
		task:destroy()
	elseif task.disconnect then
		task:disconnect()
	else
		error("Unable to cleanup unknown task")
	end
end

function Maid:clean()
	local tasks = self._tasks

	for key,task in pairs(tasks) do
		if typeof(task) == 'RBXScriptConnection' then
			tasks[key] = nil
			task:Disconnect()
		end
	end

	local index, task = next(tasks)
	while task ~= nil do
		tasks[index] = nil
		Maid.cleanupTask(task)
		index, task = next(tasks)
	end
end

Maid.destroy = Maid.clean
Maid.Destroy = Maid.clean

return Maid

Yes or put it in the same if statement as the distance code.
So this part:

So it would be

if distance <= ATTACK_RADIUS and targetInstance.Parent:FindFirstChild("HumanoidRootPart") then
   ...
end

What you got should work as well, please put a post as solution if it does, so that this post gets shelved. :smile:

2 Likes

Like this?

local function isInstaceAttackable(targetInstance)
	local targetHumanoid = targetInstance and targetInstance.Parent and targetInstance.Parent:FindFirstChild("Humanoid")
	if not targetHumanoid then
		return false
	end

	local isAttackable = false
	local distance = (maid.humanoidRootPart.Position - targetInstance.Position).Magnitude
		if distance <= ATTACK_RADIUS and targetInstance.Parent:FindFirstChild("HumanoidRootPart") then -- run the code
		local ray = Ray.new(
			maid.humanoidRootPart.Position,
			(targetInstance.ParentHumanoidRootPart.Position - maid.humanoidRootPart.Position).Unit * distance
		)

		local part = Workspace:FindPartOnRayWithIgnoreList(ray, {
			targetInstance.Parent, maid.instance,
		}, false, true)

		if
			targetInstance ~= maid.instance and
			targetInstance:IsDescendantOf(Workspace) and
			targetHumanoid.Health > 0 and
			targetHumanoid:GetState() ~= Enum.HumanoidStateType.Dead and
			not CollectionService:HasTag(targetInstance.Parent, "ZombieFriend") and
			not part
		then
			isAttackable = true
		end
	end
	return isAttackable
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.