Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there anyway to tell your script to wait for all Descendants before continuing?

This is somewhat related to another question I had. I tried using ChildAdded to fix this issue, but all it does is give me even bigger issues. So I decided to try another way.

Right now I have a script that removes all head-related accessories on a player, but the only issue is that it seems to not be able to find them since it runs the if statements that check for "Accessories" before they've even spawned on the Character.

Because of this, the Player spawns in with the helmet they wore when they left the game (what I want) but they still have their head Accessories on, which is causing clipping.

So this is some of my code:

player.CharacterAdded:Connect(function(char)
	if char:findFirstChild("Humanoid") ~= nil then
				-- removes all accessories on face
				for i, v in pairs(char:GetDescendants())do 
					if v:IsA('Accessory') then
						local attach = v:FindFirstChildWhichIsA('Attachment', true)
						if attach and namelist[attach.Name] then
							v:Destroy()
							print('REMOVED:', v)
						end
					elseif v:IsA('Hat') then 
						v:Destroy()
					end
				end

This is not all the code, but this is what's causing the issue. I've added print statements inside the v:IsA('Accessory') and they don't print, which has lead me to believe that they haven't spawned yet but the code has already run. This is why the accessories are not removed. Is there anyway to set this up so it waits for all the descendants in the char:GetDescendants() part?

1 answer

✓ Accepted answer

I wouldn't recommend using :CharacterAdded() as it fires the moment the instance is in workspace.

As an alternative; to wait until the character is FULLY loaded (hats, torso, humanoid, etc); I'd suggest using :CharacterAppearanceLoaded()

view here - https://developer.roblox.com/api-reference/event/Player/CharacterAppearanceLoaded

Considering your script is properly functioning; all you simply have to do is replace CharacterAdded to CharacterAppearanceLoaded

player.CharacterAppearanceLoaded:Connect(function(char)
    if char:findFirstChild("Humanoid") ~= nil then
                -- removes all accessories on face
                for i, v in pairs(char:GetDescendants())do 
                    if v:IsA('Accessory') then
                        local attach = v:FindFirstChildWhichIsA('Attachment', true)
                        if attach and namelist[attach.Name] then
                            v:Destroy()
                            print('REMOVED:', v)
                        end
                    elseif v:IsA('Hat') then 
                        v:Destroy()
                    end
                end

Hope this helps.

1 comment

0

Thank you! That fixed my problem.

This discussion is preserved as it was. New answers, comments, and votes are closed.