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
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.
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.