Check if something doesn't exist

I want to make a script where it checks whether a model has a decal in it. If it doesn’t, code will run. How would I do this?

2 Likes
if Model:FindFirstChild("Decal") == nil then
   --Do something here
end

Just basically implement a conditional check if the Decal you’re attempting to find for inside the Model is equal to nil (Or like you said, something that doesn’t exist)

6 Likes

Just to clarify, does FindFirstChild() find the object with the class name decal, or an object with with the name “Decal”?

Instance:FindFirstChild() looks for an Instance with the given name – if you’d like to look for the first child of an Instance with a given ClassName, utilizing Instance:FindFirstChildOfClass() or Instance:FindFirstChildWhichIsA() would be required, instead.

2 Likes

The decal isn’t actually a child, its a descendant. What would I use instead?

Probably this (Thanks formatting)

Maybe you could implement it like this?

if not Model:IsDescendantOf("Decal") then
    --Do your stuff
end
1 Like

The OP is also trying to check if a decal is a descendant of the model and not vice versa. Additionally, the accepted parameter of that method is an Instance and not a ClassName, which means that the OP would need to have a reference to the Decal before checking if it’s contained within the Model.

This is demonstrated by the example code on its Developer Hub page:

local part = Instance.new("Part")
print(part:IsDescendantOf(game))
--> false
 
part.Parent = game.Workspace
print(part:IsDescendantOf(game))
--> true
 
part.Parent = game
print(part:IsDescendantOf(game))
--> true

In this case, a loop could be utilized to determine if a decal is a descendant of the model:

local Model = script.Parent

for _, item in ipairs(Model:GetDescendants()) do
    if item:IsA("Decal") then -- If a descendant on its current iteration is a decal...
        -- Continue
    end
end
2 Likes