Why am I getting this WaitForChild() error?

Hello. I am using a conveyor script and it keeps giving me this error:

Even when this error is occurring, everything still works, the only issue is that the error spams so much that it lags the player’s game very badly.


Here is the code:

local addConveyor = function(c)
	if c.Name == "CONVEYOR" then
		
		local con
		
		con = game:GetService("RunService").RenderStepped:Connect(function()
			
			if not c.Parent then 
				con:Disconnect() 
			end
			
			c.Parent:WaitForChild("ConveyorTexture").OffsetStudsV = -(workspace.DistributedGameTime*c.Value)%(c.Parent.ConveyorTexture.StudsPerTileV)
			
		end)
		
	else
		-- still need to add stuff here.
	end
end

workspace.DescendantAdded:Connect(addConveyor)
for i,v in pairs(workspace:GetDescendants()) do
	addConveyor(v)
end

I do not know why I am getting this error though, because every conveyor has the “CONVEYOR” value in it, and it has the “ConveyorTexture” in… yet the error still persists.
image


I have tried everything I know:

  • I have changed it to FindFirstChild() to see if that helped. nope.
  • When the error has appeared, I have checked every conveyor and both the “CONVEYOR” value and “ConveyorTexture” texture are in each one and named correctly.

Any help would be so good.

The error you are getting tell me that the thing your trying to get the child of it no a thing, check if it is right.

Should you put an else statement (containing the WaitForChild part) after the if not c.Parent then... block since that’s catching the scenario of there being no parent?

Rather than waiting every time, check if it’s there. If it is, change the offset.

con = game:GetService("RunService").RenderStepped:Connect(function()
			
    if not c.Parent then 
        con:Disconnect() 
    end
    
    if c.Parent:FindFirstChild("ConveyorTexture") then
        c.Parent.ConveyorTexture.OffsetStudsV = -(workspace.DistributedGameTime*c.Value)%(c.Parent.ConveyorTexture.StudsPerTileV)
    end
    
end)

getting this error when I do that:

Have you also implemented MP3Face’s idea?

Try this

local addConveyor = function(c)
	if v.Name == "CONVEYOR" and v.Parent and v.Parent:FindFirstChild("ConveyorTexture") then
		local con
		
		con = game:GetService("RunService").RenderStepped:Connect(function()
			if not c.Parent then 
				con:Disconnect() 
			end
			
			c.Parent:WaitForChild("ConveyorTexture").OffsetStudsV = -(workspace.DistributedGameTime*c.Value)%(c.Parent.ConveyorTexture.StudsPerTileV)
		end)
	else
		-- still need to add stuff here.
	end
end

workspace.DescendantAdded:Connect(addConveyor)

for i,v in pairs(workspace:GetDescendants()) do
	addConveyor(v)
end

Just because you disconnect the connection after it doesn’t find a parent, doesn’t mean it’ll exit on the current frame. It will still go to your line 12 and try to find a parent attribute which it doesn’t have. I’ll demonstrate…


con = game:GetService("RunService").RenderStepped:Connect(function()
	if not script.Parent:FindFirstChild("ooga") then 
		con:Disconnect()
		print("yeah connection disconnected")
	end
	print("still here")
	script.Parent:WaitForChild("ConveyorTexture").OffsetStudsV = -(workspace.DistributedGameTime*c.Value)%(c.Parent.ConveyorTexture.StudsPerTileV)
end)

Kind of a different scenario in this example here but I hope you get the picture.

You need to think about a logic like maybe put a return inside the if not statement depending on how your logic works or just an else statement like others have suggested…

still getting the error…
. . . . . . . . . . . . . . . . . . . . . . . .

How about this?

local addConveyor = function(c)
	if v.Name == "CONVEYOR" and v.Parent and v.Parent:FindFirstChild("ConveyorTexture") then
		local con
		
		con = game:GetService("RunService").RenderStepped:Connect(function()
			if c.Parent and c.Parent:FindFirstChild("ConveyorTexture") then 
				c.Parent:WaitForChild("ConveyorTexture").OffsetStudsV = -(workspace.DistributedGameTime*c.Value)%(c.Parent.ConveyorTexture.StudsPerTileV)
			else
				con:Disconnect() 
			end
		end)
	else
		-- still need to add stuff here.
	end
end

workspace.DescendantAdded:Connect(addConveyor)

for i,v in pairs(workspace:GetDescendants()) do
	addConveyor(v)
end

write return after con:Disconnect() in your original script…

I believe this is working now.
Thanks heaps :slight_smile: