Along these months i noticed something, when i write a script to reference a instance, or something, like the player too, It doesn’t works, Because Somehow, i don’t know if the script or instance loads first, but the script says that the referenced instance Is nil or null, That makes me wonder, is there any Way to prevent this?, maybe this happens because i don’t have Good components on my pc, and it takes such a long time to load the game, but you see
oh yeah, I also did a task.wait(0.1) on some scripts, but it still does that
Make sure you are using :WaitForChild(nameOfInstsance)
If you are trying to reference a model and workspace.StreamingEnabled is set to true, you can set the model’s ModelStreamingMode to Persistent via the properties and explorer windows.
If you are trying to reference an instance of a specific Class, you can use this function
local function WaitForChildOfClass(parent, class)
local child = parent:FindFirstChildOfClass(class)
while not child or child.ClassName ~= class do
child = parent.ChildAdded:Wait()
end
return child
end
This could be useful when trying to find Humanoid classes, but you can also simply use :WaitForChild("Humanoid") in that case.
Can you show an example of where it doesn’t work? It should work, make sure for important instances they have more specific name other than “sphere” as there may be other children with that name in workspace
I Certainly can’t show a “example” sadly, It only replicates when I test the game, And sometimes, But let’s just says that it yields or just prints the instance it’s nil, this occurs more often when the game takes too much on loading, And also more more often on Local Scripts
WaitForChild() bassicly waits an infinite time until it finds the instance you’re trying to find
ex:
local gyat = workspace:FindFirstChild("Sphere") -- if not found returns nil
local rizz = workspace:WaitForChild("Part") -- waits forever until found or inf yield
print("ohio") -- won't print until rizz is found
Anyways, If you want, you can try it by yourself, Look at the Client on developer console
it will probably just yield many times, or infinite yield, it won’t stop, atleast that’s my case
The Menu Main Code that it’s yielding is this one :
-- !Strict
-- hola!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
print("ContentService loading")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local ContentProvider = game:GetService("ContentProvider")
local replicatedStorage = game:GetService("ReplicatedStorage")
local eventsfolder = replicatedStorage:WaitForChild("Remotes", 5)
local SetCharFrame = eventsfolder:WaitForChild("SetCharFrame", 5)
local modules = replicatedStorage:WaitForChild("Modules", 3)
local gui = script.Parent
local loadingFrame = gui:WaitForChild("LoadingFrame")
local plr = game:GetService("Players")
local localplr = plr.LocalPlayer
ReplicatedFirst:RemoveDefaultLoadingScreen()
print("Default loading screen removed")
loadingFrame.Visible = true
SetCharFrame:FireServer("menubegin")
warn("Check 1 avanzar")
--task.wait(12.7)
local startTime = os.clock()
local descendantsTable = {}
local descendants = game:GetDescendants()
for _, descendant in game:GetDescendants() do
table.insert(descendants, descendant)
end
for index=1, #descendants do
loadingFrame.assets.Text = "LOADING ASSETS: " .. tostring(index) .. "/" .. tostring(#descendants)
ContentProvider:PreloadAsync(descendantsTable)
if Random.new():NextNumber(0, #descendants / 500) <= 1 then
task.wait()
end
end
task.wait(1)
if game:IsLoaded() then
local MainMusicFolder = workspace:WaitForChild("MainMusic") or workspace:FindFirstChild("MainMusic")
local eventsfolder = replicatedStorage:WaitForChild("Remotes", 5)
local SetCharFrame = eventsfolder:WaitForChild("SetCharFrame", 5)
WaitForChild is not neccesary here even if you have a slow device. Make sure the names are capitalized correctly and the actual instances are present in ReplicatedStorage
Everything it’s correct, but it just stops here, Won’t load anymore (ignore the errors, not important), also cam rig it’s just for the camera animations
if game:IsLoaded() then
local MainMusicFolder = workspace:WaitForChild("MainMusic") or workspace:FindFirstChild("MainMusic")
local ColdIntro2 = MainMusicFolder:WaitForChild("Cold2")
local ColdLeitmotif = MainMusicFolder:WaitForChild("ColdLeitmotif")
local tweenService = game:GetService("TweenService")
local ViewportManager = require(replicatedStorage:WaitForChild("ViewportManager"));
local Wind1 = MainMusicFolder:WaitForChild("wind")
local Wind2 = MainMusicFolder:WaitForChild("wind2")
-- REMOTEEVENTS
--
local EnableViewportChar = false
local RunS = game:GetService("RunService")
local cam = workspace.CurrentCamera
--repeat task.wait() until game:IsLoaded()
local buttons = gui:WaitForChild("Buttons")
local players = game:GetService("Players")
local localplayer = players.LocalPlayer
local playbutton = buttons:WaitForChild("Play")
local titulo = gui:WaitForChild("tel")
local transicionNegrura = gui:WaitForChild("trans")
local gametitle = gui:WaitForChild("tel")
-- CAM VARIABLES
local animation = script:WaitForChild("CameraAnimation")
local MenuFolder = workspace:WaitForChild("Menu")
local camRig = MenuFolder:WaitForChild("CamRig")
local camPart = camRig:WaitForChild("CameraPart")
local controller = camRig:WaitForChild("AnimationController")
local animator = controller:WaitForChild("Animator")
local loadanim = animator:LoadAnimation(animation)
--
local character = localplayer.Character or localplayer.CharacterAdded:Wait()
character:WaitForChild('Humanoid');
Line 84 would be this one >
local camPart = camRig:WaitForChild(“CameraPart”)
Is CameraPart a child of script.CameraAnimation.Menu.CamRig?
Can you show the explorer window of that?
And if it is, make sure the CameraPart.Anchored is set to true
If you want to keep the CameraPart unanchored, you must create a WeldConstraint in the HumanoidRootPart.
Set the Part0 of the WeldConstraint to the HumanoidRootPart
Then, set the Part1 of the WeldConstraint to the CameraPart
I’m sure the motor6d fulfills a similar function to the one you mentioned, Also, I have noticed when testing the game, when I access the Explorer when being on the client, the CamPart is still there, intact
Ok you can go back to Motor6Ds. Can you do Ctrl + Shift + F on Studio and search for :WaitForChild("CameraPart")
and send a screenshot of what it looks like
If what I said doesn’t help. I have a temporary solution that’s kind of hacky but should work.
You can use CollectionService & the Tag Editor to add a tag to the menu camera part
local CollectionService = game:GetService("CollectionService")
local MENU_CAMERA_PART_Tag = "_MenuCameraPart"
if #CollectionService:GetTagged(MENU_CAMERA_PART_Tag) < 1 then
repeat task.wait() until #CollectionService:GetTagged(MENU_CAMERA_PART_Tag) >= 1
end
local cameraPart = CollectionService:GetTagged(MENU_CAMERA_PART_Tag)[1]
print(cameraPart)
I tried this, but it still didn’t work, on the server it prints normally, but it doesn’t on the client, and on the client the part it’s there but on the local script says it doesn’t exists, very weird