Ad
Log in to vote
0

How do I check if an object exists? [closed]

Asked by 3 years ago

Whenever I run code, sometimes the object doesn't exist like a characters's Left Leg. How do I check if it's there? Whenever I do:

if character["Left Leg"] ~= nil then
    --stuf
end

it gives me an error. Help?

Locked by Thewsomeguy and Articulating

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Hi.

Looks like you're using an ad blocker.

That's fine...

No, it really is.

Just, I mean, we put a lot of work into this site, you know?

It would be really really appreciated if you would turn off your ad blocker for our website...

We tried really hard to make our ads as unobtrusive as possible.

If you really hate ads, would you consider a $2 donation via Patreon?

Here's the link.

We love you. We hope you love us too.

.

..

...

Now back to your regularly scheduled Scripting Helpers....

...

..

.

6 answers

Log in to vote
5
Answered by
Everesty 976
3 years ago

Use the FindFirstChild method. The FindFirstChild method will search through the children of an object, until it finds an object who's name is the same as the string supplied, then it returns that object. If it doesn't find anything, it returns nil. Example:

if character:FindFirstChild("Left Leg") ~= nil then
    -- stuff
emd
Ad
Log in to vote
4
Answered by
Merely 1980
3 years ago

Adding on to 18cwatford's answer, in some cases you may want to wait until the object is created. Use the WaitForChild method.

Example:

local leftLeg = character:WaitForChild("Left Leg")

If the "Left Leg" object is not added, then the script will wait forever.

Log in to vote
3
Answered by
blocco 150
3 years ago

Adding on to Merely's answer, WaitForChild only works if an object is added already having a certain name. That is, changing an object's name to the one you are waiting for after setting its parent will not count and your script will still be halted.

This may seem trivial at the moment, but you may come across times when you accidentally set an object's parent and then name it, rendering the WaitForChild method ineffective.

Log in to vote
0
Answered by 3 years ago
local model= workspace:FindFirstChild("Model") --Find Model
if not model~=nil then --Does model exist or not?
print("Oh no! Model doesn't exist!") --It doesn't exist! :O
else --Elseif it does
print("Yay! Model exists!") --Yay! It exists! ;D
end --The end for all the 'if' loop(s)
0
You can learn this kind of scripting from Person299's Admin Commands. TheeDeathCaster 1884 — 3y
Log in to vote
-1
Answered by 3 years ago

To make the code a bit more efficient, you could do

LLeg = Workspace.Player:FindFirstChild("Left Leg")
if LLeg then
--Insert Code Here
end
Log in to vote
-2
Answered by 3 years ago

Sure you could do all these fancy ~= nil things, but I like to do it like this:

if character["Left Leg"] then -- You don't need to use ~= nil, it checks if you do it like this.
-- Code
end

Well, bye.

  • Kratos232
1
That will just error if the object doesn't exist. Learn how to use something before spreading false information. User#11893 65 — 3y
0
It doesn't error, if you actually tried it you'd see it didn't. I use this in all my scripts and they all work fine. Kratos232 50 — 3y
Ad