Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
Kami_Yo 72
5 years ago

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

Log in to vote
1
Answered by
Astralyst 389 Moderation Voter
5 years ago
Edited 5 years ago

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.

0
Thank you! That fixed my problem. Kami_Yo 72 — 5y
Ad

Answer this question