So I am trying to find an inserted (or cloned) model in the workspace and get it's children then, I'm trying to get a random child from that and using "MoveTo" move the players character that clicked the button to it. I keep getting errors because if I use some thing like this:
local SpawnModel = game.Workspace:WaitForChild("SpawnsForMap1" or "SpawnsForMap2"):GetChildren() -- it always waits for the first model and never reads the next part -- or use this, after disabling the script and re-enabling it with another script: local SpawnModel = game.Workspace:findFirstChild("SpawnsForMap1" or "SpawnsForMap2"):GetChildren() -- same issue for this one except it outputs an error because it's not waiting it's searching and returning it found nothing for a reason I'm stumped by --Error: attempt to index a nil value -- I know what this means but don't understand how I can fix it to make it look for a set of models and only that set and if it finds one model return that model's children so the rest of the script can function and move the player to a random brick
So, could anyone let me know what I can use here that allows me to search for a lot of models but once it finds one that exists it returns that to continue the rest of the script? I feel as though I may be close to an answer but I'm not exactly sure. Would using something like this work or, something completely different?:
local SpawnModel = repeat game.Workspace:FindFirstChild("MapSpawnsName1" or "MapSpawnsName2" ) until return --Yes I know this doesn't work but I'm not sure what would...
Other:
local Maps = {"Map1","Map2" } local SpawnPos local found = false while found == false do local work = game.Workspace for _, v in pairs (Maps) do if found == false then if work:FindFirstChild(v) then found = true print (v) wait() SpawnPos = v:GetChildren() end end end wait() end --Error: attempt to call method 'GetChildren' (a nil value) --I'm assuming somehow I need to iterate this so I recognizes it is a model or something? --EDIT: Fixed the error by declaring that v = work:FindFirstChild(v) after actually finding it.
Thanks for any help!
local items = {"Model1","Model2","Model3"} -- etc. local found = false local spawnModel while found == false do local work = game.Workspace for _,v in pairs (items) do if found == false then if work:FindFirstChild(v) then found == true spawnModel = v end end end wait() end local spawn = spawnModel:GetChildren()[math.random(1,#spawnModel:GetChildren())]
To explain: * This script repeats the code inside the while loop until the variable 'found' is true. * The 'items' table represents the names of the items you want to search for. * The for loop repeats the code under it for how many values the 'items' table has. * A debounce check, so the script won't return two models. * Then, it tries to find a child in Workspace with the same name as a string in the 'items' table. * Then, it makes the 'found' value true, and returns the model found. * The final line gets a random child of 'spawnModel'.