Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a variable that won't give an error if the part doesn't exist yet?

Asked by 3 years ago

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?

1
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. Ziffixture 6913 — 3y
0
Ok, Thanks. :) CalebTheCourageous -3 — 3y
0
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) Finty_james 269 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Hi

This should work:

local x = game.Workspace:WaitForChild("TopCard")

x.Name.Changed:Connect(function() x = game.Workspace:FindFirstChild("BottomCard") end)

Ad

Answer this question