Started getting a bunch of infinite yields possible


Loading script inside RepFirst. Not sure if affects CoreGui loads?

--[[
	// NinjoOnline \\
	
	Loading
--]]

local Chat = game:GetService('Chat')
local Players = game:GetService('Players')
local ReplicatedFirst = game:GetService('ReplicatedFirst')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local StarterGui = game:GetService('StarterGui')

local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild('PlayerGui')

local Customise = script:WaitForChild('Customise')
local FurnitureStore = script:WaitForChild('FurnitureStore')
local Intro = script:WaitForChild('Intro')
local Shop = script:WaitForChild('Shop')

local Remotes = ReplicatedStorage:WaitForChild('Remotes')
local SharedModules = ReplicatedStorage:WaitForChild('SharedModules')

local function SetCore()
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.EmotesMenu, false)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
	
	StarterGui:SetCore('ResetButtonCallback', false)
	StarterGui:SetCore('TopbarEnabled', false)
end

Chat:RegisterChatCallback(Enum.ChatCallbackType.OnCreatingChatWindow, function()
    return {
		ClassicChatEnabled = true,
		BubbleChatEnabled = true,
		
		DefaultFont = Enum.Font.Gotham,
		ChatBarFont = Enum.Font.Gotham,
    }
end)

ReplicatedFirst:RemoveDefaultLoadingScreen()

PlayerGui:SetTopbarTransparency(1)

repeat 
	local Success = pcall(function()
		SetCore()
	end)
		
	wait(0.2) 
until Success

Customise.Parent = PlayerGui
FurnitureStore.Parent = PlayerGui
Intro.Parent = PlayerGui
Shop.Parent = PlayerGui

spawn(function()
	require(Customise.CustomiseControl)()
end)

spawn(function()
	require(FurnitureStore.Main.FurnitureStoreControl)()
end)

spawn(function()
	require(Shop.ShopControl)()
end)

require(Intro.IntroControl)

and then my lights is also inside RepFirst

--[[
	// NinjoOnline \\
	
	LightingControl
]]

local Lighting = game:GetService('Lighting')
local TweenService = game:GetService('TweenService')

local MinutesAfterMidnight = Lighting:WaitForChild('MinutesAfterMidnight')

local Lights = workspace:WaitForChild('Lights')

local LightsOn = false
local Morning, Night = 6 * 60, 18 * 60 -- 6am, 6pm

MinutesAfterMidnight.Changed:Connect(function(value)
    Lighting:SetMinutesAfterMidnight(value)

	if Lighting:GetMinutesAfterMidnight() >= Night or Lighting:GetMinutesAfterMidnight() <= Morning then
		-- If the lights are already on, return
		if LightsOn then return end
		
		LightsOn = true
	else
		-- If the lights are already off, return
		if not LightsOn then return end
		
		LightsOn = false
	end
	
	if LightsOn then
		-- To night lighting
		TweenService:Create(Lighting, TweenInfo.new(2), {Ambient = Color3.fromRGB(115, 102, 255)}):Play()
	else
		-- To day lighting
		TweenService:Create(Lighting, TweenInfo.new(2), {Ambient = Color3.fromRGB(158, 212, 217)}):Play()
	end
	
	Lighting.ColorShift_Top = LightsOn and Color3.fromRGB(44, 128, 255) or Color3.fromRGB(125, 112, 87)
	
	-- Set the street lights
	for _, v in pairs(Lights:GetDescendants()) do
		if v:IsA('SpotLight') then
			v.Enabled = LightsOn
			v.Parent.Material = LightsOn and Enum.Material.Neon or Enum.Material.SmoothPlastic
			v.Parent.BrickColor = LightsOn and BrickColor.new('Daisy orange') or BrickColor.new('White')
		end
		
		-- Lighthouse
		if v:IsA('Beam') then
			v.Enabled = LightsOn
			
			v.Parent.BodyAngularVelocity.AngularVelocity = LightsOn and Vector3.new(0, -0.25, 0) or Vector3.new(0, 0, 0)
			
			v.Parent.Material = LightsOn and Enum.Material.Neon or Enum.Material.SmoothPlastic
			v.Parent.BrickColor = LightsOn and BrickColor.new('Daisy orange') or BrickColor.new('White')
		end
	end
end)

An infinite yield is when a script might just be waiting for ever, this usually happens when you do

:WaitForChild()

With no time set in the (), if a WaitForChild exceeds 5 seconds it will be a infinite yield. I suggest just looking through and putting intigers into the wait brackets.

For example if you have a string and a number

workspace:WaitForChild(‘Part’,5)
So it will find the child part in the workspace, within 5 seconds.

The guy above beat me to it, but

basically the :WaitForChild() function has 2 parameters: the childName (string) and
timeOut (double, optional).

The potential Infinite yield warning arises when , if a call to this function exceeds 5 seconds without returning an instance, because then there is a possibility that the thread would yield indefinitely, like for example if you call :WaitForChild on something that is inexistent, that instance will be waited for indefinitely giving you a warning.

Example :

workspace:WaitForChild(tostring(nil))                    
-- tostring because the childName parameter only takes a string value 

Output :

image

Because nil is not a valid member of workspace and will never be , so definitely, an indefinite yield is possible, and that’s all the error means.

How to prevent :

This is where the timeOut parameter comes in ,
just specify the timeOut parameter so that instead of giving you the error , it just returns nil after timing out without the Instance being returned.


Chaining multiple :WaitForChild s is not a good practice as the function itself is less efficient compared to using :FindFirstChild and so must be used only when you’re completely uncertain about whether it will have instantiated at the time of indexing.

And what happens if it doesn’t? If I need the part later on down the script and it didn’t load within the 5 seconds won’t that then cause errors?

As I mentioned in my reply to the other person, if the item hasn’t loaded then later on down the script I’ll get errors. That’s why I use WaitForChild() and not FindFirstChild() as I need the item to load before it continues, that way everything in the game has properly loaded in before the player can start playing

As I said, don’t start daisy chaining this function, and either ways if you do be sure to set the timeOut parameter unless you want it not to time out and instead return an error

Using timeout feature results in an error later on down the track tho

local Lighting = game:GetService('Lighting')
local TweenService = game:GetService('TweenService')

local MinutesAfterMidnight = Lighting:WaitForChild('MinutesAfterMidnight')

local Lights = workspace:WaitForChild('Lights', 5)

local LightsOn = false
local Morning, Night = 6 * 60, 18 * 60 -- 6am, 6pm

MinutesAfterMidnight.Changed:Connect(function(value)
    Lighting:SetMinutesAfterMidnight(value)

	if Lighting:GetMinutesAfterMidnight() >= Night or Lighting:GetMinutesAfterMidnight() <= Morning then
		-- If the lights are already on, return
		if LightsOn then return end
		
		LightsOn = true
	else
		-- If the lights are already off, return
		if not LightsOn then return end
		
		LightsOn = false
	end
	
	if LightsOn then
		-- To night lighting
		TweenService:Create(Lighting, TweenInfo.new(5), {Ambient = Color3.fromRGB(115, 102, 255)}):Play()
	else
		-- To day lighting
		TweenService:Create(Lighting, TweenInfo.new(5), {Ambient = Color3.fromRGB(158, 212, 217)}):Play()
	end
	
	Lighting.ColorShift_Top = LightsOn and Color3.fromRGB(44, 128, 255) or Color3.fromRGB(125, 112, 87)
	
	-- Set the street lights
	for _, v in pairs(Lights:GetDescendants()) do -- ERROR HERE
		if v:IsA('SpotLight') then
			v.Enabled = LightsOn
			v.Parent.Material = LightsOn and Enum.Material.Neon or Enum.Material.SmoothPlastic
			v.Parent.BrickColor = LightsOn and BrickColor.new('Daisy orange') or BrickColor.new('White')
		end
		
		-- Lighthouse
		if v:IsA('Beam') then
			v.Enabled = LightsOn
			
			v.Parent.BodyAngularVelocity.AngularVelocity = LightsOn and Vector3.new(0, -0.25, 0) or Vector3.new(0, 0, 0)
			
			v.Parent.Material = LightsOn and Enum.Material.Neon or Enum.Material.SmoothPlastic
			v.Parent.BrickColor = LightsOn and BrickColor.new('Daisy orange') or BrickColor.new('White')
		end
	end
end)

Since it didn’t wait for the Lights to load in workspace, now when I try to do Lights:GetDescendants() it errors, saying that Lights is nil.

1 Like