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:
01 | local SpawnModel = game.Workspace:WaitForChild( "SpawnsForMap1" or "SpawnsForMap2" ):GetChildren() |
02 | -- it always waits for the first model and never reads the next part |
03 |
04 | -- or use this, after disabling the script and re-enabling it with another script: |
05 |
06 | local SpawnModel = game.Workspace:findFirstChild( "SpawnsForMap1" or "SpawnsForMap2" ):GetChildren() |
07 |
08 | -- 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 |
09 |
10 | --Error: attempt to index a nil value |
11 |
12 | -- 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?:
1 | local SpawnModel = repeat game.Workspace:FindFirstChild( "MapSpawnsName1" or "MapSpawnsName2" ) until return |
2 |
3 | --Yes I know this doesn't work but I'm not sure what would... |
Other:
01 | local Maps = { "Map1" , "Map2" } |
02 | local SpawnPos |
03 | local found = false |
04 | while found = = false do |
05 | local work = game.Workspace |
06 | for _, v in pairs (Maps) do |
07 | if found = = false then |
08 | if work:FindFirstChild(v) then |
09 | found = true |
10 | print (v) |
11 | wait() |
12 | SpawnPos = v:GetChildren() |
13 | end |
14 | end |
15 | end |
Thanks for any help!
01 | local items = { "Model1" , "Model2" , "Model3" } -- etc. |
02 | local found = false |
03 | local spawnModel |
04 |
05 | while found = = false do |
06 | local work = game.Workspace |
07 | for _,v in pairs (items) do |
08 | if found = = false then |
09 | if work:FindFirstChild(v) then |
10 | found = = true |
11 | spawnModel = v |
12 | end |
13 | end |
14 | end |
15 | wait() |
16 | end |
17 |
18 | 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'.