Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is this a necessary thing to load a value?

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by 5 years ago

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!

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. magicguy78942 238 — 5y
0
Then just leave the second parameter blank and it will wait forever until it finds that child. JackOfAllBlox 32 — 5y
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. magicguy78942 238 — 5y
0
The WaitForChild() alone should be fine. If it times out there;s likely an error, no child, or your computer is a potato. TiredMelon 405 — 5y
Ad

Answer this question