I want to get a characters shirt every time (if they have a shirt), but my problem is FindFirstChild() doesnt wait for it to be added and if the player doesnt have a shirt then WaitForChild() breaks the script, how could i fix this problem?
--HELP!!
local Shirt = char:FindFirstChildWhichIsA("Shirt")
The best solution I have found is to wait for the character to be part of Workspace.
Example:
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local function WorkspaceChildAdded(Child)
if Child:IsA("Model") then
local Player = Players:GetPlayerFromCharacter(Child)
if Player then
print("Player =", Player.Name)
local Character = Player.Character
local Shirt = Character:FindFirstChildWhichIsA("Shirt")
if Shirt then
print("Shirt Found", Shirt)
end
else
print("Not a player.")
end
end
end
Workspace.ChildAdded:Connect(WorkspaceChildAdded)
local function PlrAdded(player)
local newShirt
local connection = player.CharacterAppearanceLoaded:Connect(function(character)
local plrShirt = character:FindFirstChildWhichIsA("Shirt")
if newShirt then
newShirt = plrShirt
end
end)
print("DOING")
player.AncestryChanged:Wait()
print("FINE")
connection:Disconnect()
return newShirt
end
local shirt = PlrAdded(game.Players.LocalPlayer)
local function getShirt(plr : Player)
if not plr:HasAppearanceLoaded() then
plr.CharacterAppearanceLoaded:Wait()
end
return plr.Character:FindFirstChildOfClass("Shirt")
end
local shirt = getShirt(game.Players.LocalPlayer)