WaitForChild not yielding for long enough

I’m trying to use raycasting to see detect a certain model, and I do that check every time a model is cloned and added to a model. However, I need to make sure that it detects the part, so I use WaitForChild(part). It does not yield long enough, so the raycast returns nil. I know that it is not a problem with the raycasting since I put in a wait(1), and it detected the model just fine. Is there way to delay the raycast long enough so it actually detects the model without using wait()?

Script

workspace.Base.ItemHolder.ChildAdded:Connect(function(child)
	if cS:HasTag(child, "Conveyor") then
		--wait(1)
		local thing = child:WaitForChild("ConveyorDetector")
		local hit = raycastModule.CheckConveyor(detector, distance, script.Parent)
		if hit then
			connection = hit
		end
	end
end)
1 Like

WaitForChild has a second parameter for it’s max yield time. So instead of child:WaitForChild("ConveyorDetector") it should be something more like child:WaitForChild("ConveyorDetector", some number).

1 Like

I set that number to 100, and it still doesn’t work. I don’t think the max yield time is a problem since it doesn’t throw the “infinite yield possible” warning.

I think I may have misunderstood your question, are you trying to find the conveyor detector with a ray or is the conveyor detector the ray origin?

1 Like

I am trying to find the part with the ray. Also, I believe that the problem with the WaitForChild() is that when the model is added and its children are added, it isn’t positioned yet, and the raycast is executed faster than the model positioned correctly. (Actually I don’t this is the case woops)

To make sure that doesn’t happen then you should position the model after you define it as a variable.

Prior to parenting the model to the workspace, I already set its primaryPart CFrame. I’m not sure if it’s the model not being positioned thats the problem.

If the thing you’re trying to detect is a model, then the ray is only detecting a part inside of the model. Maybe trying seeing if the object is a descendant of the conveyor detector.