Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to check if something exists?

wait(10)
x=script.Parent[""..script.Parent.Parent.Parent.OwnerName.Value.."'s WarClone"]:clone()
if game.kServerStorage[""..script.Parent.Parent.Parent.OwnerName.Value.."'s Clones"] == nil
	then h = Instance.new("Model")
	h.Parent = game.kServerStorage
	h.Name = ""..script.Parent.Parent.Parent.OwnerName.Value.."'s Clones"
	x.Parent = game.kServerStorage[""..script.Parent.Parent.Parent.OwnerName.Value.."'s Clones"]
else x.Parent = game.kServerStorage[""..script.Parent.Parent.Parent.OwnerName.Value.."'s Clones"]
end

kServerStorage is the ServerStorage just renamed...

I cant manage to make it to work, becuase when this script runs, the part that it check for being nil IS nil, how do I check if it is 'there', because whenever I check if it is 'there' it says in the output 'error stuffs... this thingy doesn't exist!'

So how do I check if it exists? I thought it was something like:

if game.kServerStorage[""..script.Parent.Parent.Parent.OwnerName.Value.."'s Clones"] == nil

By the way, the problem is with this part ^

Please help, thanks!

1 answer

✓ Accepted answer

If try something like

if workspace.Part == nil then

... that won't work. Instead of getting nil, an error will happen ("no such property 'Part' in Workspace") and it won't even get to checking your if. So we can't use . (or equivalently []) to check if an object exists.

Instead, you can use :FindFirstChild, which just returns nil if the thing doesn't exist:

if workspace:FindFirstChild("Part") == nil then

It's fairly idiomatic in Lua to use drop == nil or ~= nil when it's superfluous. To see if something is not nil, you would use not:

if not workspace:FindFirstChild("Part") then

You can read that as "if I don't find 'Part' in workspace", or "if 'Part' doesn't exist in workspace".


I would really recommend not using the "Name's Clones" format, since it's so much more complicated than necessary. Instead, I would suggest having a "Clones" folder in the ServerStorage and referencing ServerStorage.Clone[name].

I would also highly recommend script.Parent.Parent.Parent to a variable instead of repeating it all over the place. Possibly saving script.Parent.Parent.Parent.OwnerValue to a variable, too.

5 comments

0

This is a tycoon that's why it HAS to be "Name's Clones" :P http://www.roblox.com/games/51446015/NEW-Cloning-Facility-Tycoon-Alpha-Version-1-4

0

Thanks for help! :D

0

I just tested and it worked I appreciate all the helpful info you gave me thanks. I was getting desperate and was going to the waitForChild( , ) function or whatever it is

0

While they're in the ServerStorage they don't have to be -- they only have to be named while they're visible in the workspace! Rename them when you drop them there.

0

Thanks, this actually helped me out. This is the only way I could show my thanks because I can't upvote your answer because of my low reputation.

This discussion is preserved as it was. New answers, comments, and votes are closed.