Unable to cast value to objects

I can’t seem to understand the error and why is it occuring. Please solve!

local hum = script.Parent.Humanoid
local root = script.Parent.HumanoidRootPart
local head = script.Parent.Head

local grab = script.Parent.Grab
local grabAnim = hum:LoadAnimation(grab)
grabAnim.Priority = Enum.AnimationPriority.Action

local grabSound = head:WaitForChild("Attack")
local screamLoud = head:WaitForChild("Scream")

local clone = script.Parent:Clone()

function walkRandomly()
	local xRand = math.random(-50,50)
	local zRand = math.random(-50,50)
	
	local goal = root.Position + Vector3.new(xRand,0,zRand)
	
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(root.Position, goal)
	local waypoints = path:GetWaypoints()
	
	if path.Status == Enum.PathStatus.Success then
		for i,v in pairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				hum.Jump = true
			end
			
			hum:MoveTo(v.Position)
			local timeOut = hum.MoveToFinished:Wait(1)
			if not timeOut then -- checks if npc is stucked
				print("Stucked.")
				hum.Jump = true
				walkRandomly()
			end
		end
	else
		print("Path failed.")
		wait(1)
		walkRandomly()
	end
end

function findPath(target)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(root.Position,target.Position)
	local waypoints = path:GetWaypoints()
	
	if path.Status == Enum.PathStatus.Success then
		for i,v in pairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				hum.Jump = true
			end
			
			hum:MoveTo(v.Position)
			local timeOut = hum.MoveToFinished:Wait(1)
			if not timeOut then
				hum.Jump = true
				print("Path too long!")
				findPath(target)
				break
			end
			if checkSight(target) then
				repeat
					print("moving directly to target")
					hum:MoveTo(target.Position)
					wait(0.1)
					if target == nil then
						break
					elseif target.Parent == nil then
						break
					end
				until checkSight(target) == false or hum.Health < 1 or target.Parent.Humanoid.Health < 1
				break
			end
			if (root.Position - waypoints[1].Position).magnitude > 20 then
				print("target has moved, generating new path...")
				findPath(target)
				break
			end
		end
	end
end

function checkSight(target) -- checks if zombie has a clear sight of target, ex: target have same y axis
	local ray = Ray.new(root.Position,(target.Position - root.Position).Unit * 40)
	local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, script.Parent)
	if hit then
		if hit:IsDescendantOf(target.Parent) and math.abs(hit.Position.Y - root.Position.Y) < 3 then
			print("target found")
			return true
		end
	else
		return false
	end
end

function findTarget()
	local dist = 200
	local target = nil
	local potentialTargets = {}
	local seeTargets = {}
	
	for i,v in ipairs(workspace:GetChildren()) do
		local hum = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
		
		if hum and torso and v.Name ~= script.Parent.Name then
			if (root.Position - torso.Position).magnitude < dist and hum.Health > 0 then
				table.insert(potentialTargets,torso)
			end
		end
	end
	if #potentialTargets > 0 then
		for i,v in ipairs(potentialTargets) do
			if checkSight(v) then
				table.insert(seeTargets,v)
			elseif #seeTargets == 0 and (root.Position - v.Position).magnitude < dist then
				target = v
				dist = (root.Position - v.Position).magnitude
			end
		end
	end
	if #seeTargets > 0 then
		dist = 200
		for i,v in ipairs(seeTargets) do
			if (root.Position - v.Position).magnitude < dist then
				target = v
				dist = (root.Position - v.Position).magnitude
			end
		end
	end
	if target then
		if math.random(20) == 1 then
			screamLoud:Play()
		end
	end
	return target
end

function Main()
	local target = findTarget()
	if target then
		hum.WalkSpeed = 16
		findTarget(target)
	else
		hum.WalkSpeed = 8
		walkRandomly()
	end
end

while wait(0.1) do
	if hum.Health < 1 then
		break
	end
	Main()
end
local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, script.Parent)

This is the error of the script, located under checkSight() function.

local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})

1 Like