Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Would I use a table or, something else?

Asked by 8 years ago

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!

1 answer

Log in to vote
0
Answered by 8 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
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'.

0
Thanks, but I wanted to get the children inside that model, apparently after some testing I get several errors, do you know how this would work with ":GetChildren()"? Because what I want to do it get the children inside the model and use math.random to select one of them (I've already got the random selection figured out. Evadable 65 — 8y
0
Something like this spawnModel = work:GetChildren(v) gives me an error: attempt to get length of upvalue 'spawnModel' (a nil value) . Evadable 65 — 8y
0
Nevermind, I think I can work this through thanks very much! Evadable 65 — 8y
0
ill edit TheDeadlyPanther 2460 — 8y
0
See Other code in original post is what I've gotten. Evadable 65 — 8y
Ad

Answer this question