How do I make a script ignore non-existant values?

Hello again, I’ve been working for the last few days and I come across a new issue. I’m trying to make a script that detects a part and if that part doesn’t exist then the script just ignores it and goes on. Problem is that the script keeps throwing the “is not a valid member of Model” or something, I forgot. Here’s the example script.

if game.Workspace.TestFrame.Colors then
for i, v in pairs(game.Workspace.TestFrame.Colors:GetChildren()) do
if v.Name == “Color” then
print(“Color exist”)
elseif v == nil then
print(“Color doesn’t exist”)
end
end
end

if workspace:FindFirstChild("TestFrame"):FindFirstChild("Colors") then
  for i, v in workspace.TestFrame.Colors:GetChildren() do
    if v.Name == “Color” then
      print(“Color exist”)
    elseif v == nil then
      print(“Color doesn’t exist”)
    end
  end
end

Try this

It seems like it worked but nothing printed out on the output.

Are you sure the TestFrame and Colors exist in the workspace? If they are adding after the game is loaded you can try adding WaitForChild

workspace:WaitForChild("TestFrame"):WaitForChild("Colors")
if workspace:FindFirstChild("TestFrame"):FindFirstChild("Colors") then
  for i, v in workspace.TestFrame.Colors:GetChildren() do
    if v.Name == “Color” then
      print(“Color exist”)
    elseif v == nil then
      print(“Color doesn’t exist”)
    end
  end
end
1 Like

I was missing Colors folder, thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.