I have a loadstring calling to a module script (via pastebin w/ HttpGet, so I can use it across games), and within that module script there are local variables, such as:
local plr = game:GetService'Players'.LocalPlayer
local char = plr.Character
local hrp = char.HumanoidRootPart
When these are loaded, theres an error saying they are calling to a nil value. See, to fix the nil problem I had the script utilize a waiting call to the first line. The line is as follows:
repeat task.wait() until game:IsLoaded() and game:GetService'Players':WaitForChild'LocalPlayer'
The pattern above (WaitForChild) continues with the instances of the variables defined, I’m too lazy to type the rest lf those variables out as I am on mobile and it is 6am. (I have not slept because I want to solve this.)
I have also alterntively used FindFirstChild, and used its 2nd arg (so it utilizes descendants) to skip to searching for the HumanoidRootPart. No errors, just never loads and the script utilizing it never progresses. Previously, this script was working just fine, but when I tested it in a laggier environment (multiple clients), the nil errors occured, so I added a wait.
Bad practice. Using task.wait() with repeat to wait for code is called polling and it’s much less efficient than anything event based. And combined with :FindFirstChild() and a recursive search it wastes much more resources than necessary.
LocalPlayer reference is available right away before any local scripts run, so you can index safely. For the rest, I would use :WaitForChild().
local player = game:GetService'Players'.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hrp = char:WaitForChild'HumanoidRootPart'
Character is not a model inside a player, but a property pointing to the model that appears in workspace. Until then, it is nil.
It checks if the game is loaded already. If it was an we called :Wait(), it would indeed yield, but because of this clause it’s only going to wait if the game is not loaded yet. I’m successfully using this all the time.