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

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

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?

1 comment

0

Well, just remove the WaitForChild? Obviously that's what is stopping your code?

2 answers

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
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

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