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

How to make script keep running even when it cant find the thing its looking for?

Asked by 6 years ago
Edited 6 years ago
player.Character:WaitForChild("Shirt")
    if player.Character:FindFirstChild("Shirt") then
        player.Character.Shirt:Destroy()
    end
    if player.Character:FindFirstChild("Pants") then
        player.Character.Pants:Destroy()
    end
    if player.Character:FindFirstChild("ShirtGraphic") then
        player.Character.ShirtGraphic:Destroy()
    end

Whenever I run this script without wearing any clothing, I always get Infinite yield possible on 'DaBrainlessOne:WaitForChild("Shirt")'

any help?

0
Well, just remove the WaitForChild? Obviously that's what is stopping your code? Troidit 253 — 6y

2 answers

Log in to vote
1
Answered by
Griffi0n 315 Moderation Voter
6 years ago

I would rewrite it like this:

for index, child in pairs(player.Character:GetChildren()) do
    if child:IsA("Clothing") or child:IsA("ShirtGraphic") then
        child:Destroy()
    end
end
Ad
Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
6 years ago
Edited 6 years ago
repeat wait() 

until workspace:FindFirstChild(player.Name) -- waits until the character is a descendant of workspace or loaded

--player.Character:WaitForChild("Shirt") -- THIS WONT WORK IF THE PLAYER DOESN'T HAVE A SHIRT, THE SCRIPT WILL CONTINUE TO WAIT UNTIL A SHIRT INSTANCE IS IN THE CHARACTERS MODEL
    if player.Character:FindFirstChild("Shirt") then
        player.Character.Shirt:Destroy()
    else
    print('No Shirt Detected')
    end

    if player.Character:FindFirstChild("Pants") then
        player.Character.Pants:Destroy()
    else
    print('No Pants Detected')
    end

    if player.Character:FindFirstChild("ShirtGraphic") then
        player.Character.ShirtGraphic:Destroy()
    else
    print('No Shirt Graphic Detected')
    end

Answer this question