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