I have a variable (X = game.Workspace:FindFirstChild("BottomCard")) but a "BottomCard" doesn't exist yet because later in the script I change "TopCard" to "BottomCard". Currently it gives me an error saying that X is nil, and if I use :WaitForChild() it just stops the script indefinitely because the part won't exist until later on in the script. What can I use that doesn't stop the script?
✓ ANSWER ACCEPTED
How do I make a variable that won't give an error if the part doesn't exist yet?
1 answer
✓ Accepted answer
Hi
This should work:
local x = game.Workspace:WaitForChild("TopCard")
x.Name.Changed:Connect(function() x = game.Workspace:FindFirstChild("BottomCard") end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
You shouldn't have your program run altogether if it is relying on a part that wont exist for a decent amount of time; execute it when necessary.
Ok, Thanks. :)
If you look at the :FindFirstChild() API, you will see that if the thing that your selecting is nil, Even if it doesnt exist; The code wont alert error. WaitForChild can be used for this, Because it waits until the thing is existing or replicated (unless you add the extra parameter, if you do, that will make it wait for the certain amount of time)