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

How to wait for certain parts to load?

Asked by 4 years ago

I have this line of code here:

while plr.leaderstats == nil do wait() end

It's supposed to wait for the leaderstats to load in before running the rest of the code after. I thought of doing :WaitForChild() , but that only gives me a warning message saying 'infinite yield possible'.

The leaderstats is created in another script, and to prevent the code attempting to look for the leaderstats inside the player before it's created I had this while loop wait until it's not equal to nil.

But now I essentially have two problems right now

Problem

Why am I getting the error 'leaderstats is not a valid member of Player'

(im checking for if it's nil so in that case it's true so it should just proceed to wait right?)

and

How do I avoid 'infinite yield possible' when using :WaitForChild()? Is there a better way of waiting for certain parts to load?

i feel like the way im approaching this is making it more difficult than it should be

2 answers

Log in to vote
0
Answered by 4 years ago

That is a very bad way of waiting for something because it doesnt give you an error if something is not able to be loaded in so using waitforchild is the best way to go. For the error however it could be a spelling mistake inside the wait for child or you could have the path wrong. It would help if you show us your leaderstats script.

Ad
Log in to vote
0
Answered by 4 years ago

Your current loop will only execute the loop if it can find the leaderstats (because you're referring to something nonexistent), something that isn't desired. Try something like I got below.

repeat wait() until plr.leaderstats

Here, it'll repeat the wait until it can find the leaderstats being referred.

If this helped, consider accepting this answer for some of those sweet reputations points. If it didn't, comment below and I'll help out.

0
Still getting the error that it doesn't exist. I checked for spelling, so that's not the issue. EstrangedFisherman 51 — 4y
0
If you're still getting the error, I'm not sure it is the single line in the script that is at error. Can you check and see if your variables are appropriately defined? For example, the "plr" variable should be pointed towards the Player in the Player Service and not the workspace/character. TaxesArentAwesome 514 — 4y

Answer this question