My plugin is having an error when a Roblox Studio Place loads because the
game.DescendantAdded
event is firing when parts are loading into the game the first time. It currently works on an empty place, but when the number of instances in the Place increase, it fires. Is there any way to wait for the Place to load? Simply waiting a few seconds would be pointless, because a slow computer could take longer then that, and it would inconvenience most users.
You can use the DataModel.Loaded event (I never actually tested it but I have seen it used):
Game.Loaded:wait(); -- Waits until the Loaded event fires game.DescendantAdded:connect(function(child) print(child.Name .. " has been added."); end);
Heres something I do for the game, I use the repeat until nil
loop so that the script waits until it finds the requirements, let me show you an example;
repeat wait(0)until game and game:FindFirstChild("Workspace") --This repeats waiting until game loads, and game.Workspace is existant, might be a bit buggy though :P
That'll repeat until the game loads, and Workspace loads, but I just use the 'Workspace' coding as a just-in-case, and for game.DescentdantAdded
, you can use it for multiple purposes, for instance, to check what type it is, or what its name is, let me show you an example;
repeat wait(0)until game and game:FindFirstChild("Workspace") --Heres a 'waiting' loop game.DescentantAdded:connect(function(Object) --Here is our function if Object:IsA("Part") then --Lets check if it was a Part that was added print(Object.Name.." is a Part!") --Its a Part! end --This end is to end the code for the 'if' statement end) --And this end is to end the code for the game.DescentantAdded event/function
Now, if you want it to check the name, then;
repeat wait(0)until game and game:FindFirstChild("Workspace") --Heres a 'waiting' loop game.DescentantAdded:connect(function(Object) --Here is our function if Object.Name == "Part" then --Lets check if the name is Part print("Its a Part!") -- It is?! :o end --This end is to end the code for the 'if' statement end) --And this end is to end the code for the game.DescentantAdded event/function
As you'll see, the game.DescentantAdded
is very useful for all sorts of things! It just depends on how you use it, and the repeat until nil
loop will repeat waiting until the game loads, and Workspace loads. Hope this helped!