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

How to search all models for a name?

I have 4 models in Workspace that are named something different, I want a script to look at all models in Workspace and see if they are named either of these and if so get removed. I am trying this

local mapmodel = game.Workspace:GetChildren()
if mapmodel.Name == "Ice Map" or "Fire Map" or "Rock Map"  then
	mapmodel:remove()	
end

However in my test I am getting that :remove() is a nil value, what am I doing wrong here? Any pointers?

EDIT: This just occured to me, maybe I should rename "Ice Map" to

game.Workspace["Ice Map"] 

?

2 comments

0

Updated my asnwer... Cheers

0

My updated script works for me. Does it not for u

1 answer

✓ Accepted answer

Hi, what you want to do here is use a for loop to iterate through all of the items in your mapmodel table. A table is basically just a list of items, in your case the loop would run as many times as object that are in the workspace

local mapmodel = game.Workspace:GetChildren()

for i, v in pairs(mapmodel) do --For loop that will iterate through every item.
if v.Name == "Ice Map" or v.Name=="Fire Map" or v.Name== "Rock Map"  then
    v:Destroy() --You shouldn't use remove, it doesn't actually completely remove something   
	end
end


It is to be noted that the i and v can be named whatever you want. The standard way is to use i for index and v for value.

Since we iterate through everything in the workspace, v will eventually be the maps object, if it is then it will be destroyed!

6 comments

0

@Thetach, hey thank you for helping me! I tried it out your way, however I got "attempt to call method 'Destroy' (a nil value)" ....could it because the model name has spaces it is messing it up? I should also note that, only ONE out of the 4 maps are present in Workspace when I run this scirpt. The other 3 are in ServerStorage

0

Sorry, I edited my answer. Lua can't simply assume that in our or statements we're still talking about the objects name. Cheers. 

0

@Thetacah Hey thanks once again, I tried it this time around and it is still giving me the exact same error, I am inputting it wrong or something? I just tried copying/pasting yours directly and it still gives me the "destroy nil value" error thing, hmm....why is this? Is what I want to do not possible in LUA?

0

I just re-tried my code and it works completely fine. Are they actually models what you have in your game? Is it in a script with other code that could be affecting it?

0

@Thetacah That is incredibly strange, I must be doing something wrong here. So, each of the "Ice Map", "Fire Map", etc. are all separate Models on the root of Workspace. So Ice Map is one model in Workspace, Fire Map is another model in Workspace, etc. ONE thing I completley forgot to mention is that this is in a script that is in Workspace and that script gets called from a FireAllClients comman

0

@ Thetacah I made a typo, I put the name of the local variable instead of v for destroy...don't know why I didn't notice that but it does in fact work. THank you so much again!

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