Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is this a necessary thing to load a value?

There's a value inside the ReplicatedStorage. I want to do a statement in order to get the value if it has loaded or not. But I want to ask if is it necessary or not? Here is the script:

local storage = game:GetService('ReplicatedStorage')
local annstring = nil
if not storage:FindFirstChild('AnnoucementString') then
	annstring = storage:WaitForChild('AnnoucementString')
else
	annstring = storage.AnnoucementSring
end

If it's not really necessary. Can I use this one instead? I'm just afraid of getting infinite yield.

local storage = game:GetService('ReplicatedStorage')
local annstring = storage:WaitForChild('AnnoucementString')

Edit: If neither of my methods are incorrect or not efficient then is there any better suggestion to load the value?

1 answer

The 2nd code is more efficient... The second parameter of WaitForChild() is the maximum amount of wait time before it moves on to the next line of code.

local annstring = storage:WaitForChild('AnnoucementString', 5)

In this example, if it does not find the child AnnouncementString after 5 seconds, it will move on to the next line of code and not wait there forever. Hope this helped!

4 comments

0

Thank you for answering. But what I want is to wait for the game to load successfully before encountering any other lines of codes.

0

Then just leave the second parameter blank and it will wait forever until it finds that child.

1

Actually... That's not really how the :WaitForChild() works. If the second parameter is blank and the child doesn't appear within 10 seconds. The whole script will automatically stop the waiting yield and keeps going on without loading the value if child appears 10+ seconds late.

0

The WaitForChild() alone should be fine. If it times out there;s likely an error, no child, or your computer is a potato.

This discussion is preserved as it was. New answers, comments, and votes are closed.