generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Recursion Function

Previous Thread :: Next Thread 
NewbControl114 is not online. NewbControl114
Joined: 09 May 2009
Total Posts: 10303
02 Aug 2015 01:48 AM
function getit()
local a = workspace:GetChildren()
for b=1,#a do
if b[a]:IsA("Script") then
print(b[a])
else getit()
end
end
end

getit()

Script is supposed to recursively find all the scripts in all the children of a game, and then print their location so I can find them.


line 4 is causing me problems, which all give different errors. I've tried:

if a[b]:IsA("Script") then
--> 23:41:35.889 - Workspace.finderscript:4: attempt to call method 'IsA' (a nil value)

if b[a]:IsA("Script") then
--> 23:43:04.868 - Workspace.finderscript:4: attempt to index local 'b' (a number value)

if a:IsA("Script") then
-->23:42:27.956 - Workspace.finderscript:2: stack overflow
23:42:27.957 - Stack Begin
23:42:27.957 - Script 'Workspace.finderscript', Line 2 - global getit
23:42:27.958 - Script 'Workspace.finderscript', Line 6 - global getit
And so on and so on...

What am I doing wrong and how do I do it right?

:(
Report Abuse
instawin is not online. instawin
Joined: 04 Jun 2013
Total Posts: 8777
02 Aug 2015 01:55 AM
function rec(parent)
local children = parent:GetChildren()
local obtainedScripts = {}
for i, v in pairs(children) do
if v:IsA("LuaSourceContainer") then
table.insert(obtainedScripts, v)
else
rec(parent)
end
end
return obtainedScripts
end

this function returns a table of all the scripts found in workspace, do what you want with it

to print all the found scripts, do this:

local scriptsToCheck = rec(workspace)

for i, v in pairs(scriptsToCheck) do
print(tostring(v:GetFullName()))
end

try it, and i hope it works lel
Report Abuse
lego555444 is not online. lego555444
Joined: 21 Sep 2008
Total Posts: 1383
02 Aug 2015 01:55 AM
your getit() should have the ability to accept an instance to scan under so you can recurse
Report Abuse
instawin is not online. instawin
Joined: 04 Jun 2013
Total Posts: 8777
02 Aug 2015 01:57 AM
wait, i could see a problem here

every time we call the function to recurse deeper, i set the obtainedScripts table to an empty table each time i called it, so try this:

local obtainedScripts = {}

function rec(parent)
local children = parent:GetChildren()
for i, v in pairs(children) do
if v:IsA("LuaSourceContainer") then
table.insert(obtainedScripts, v)
else
rec(parent)
end
end
return obtainedScripts
end
Report Abuse
NewbControl114 is not online. NewbControl114
Joined: 09 May 2009
Total Posts: 10303
02 Aug 2015 01:58 AM
Instawin, your script returns stack overflow

:(
Report Abuse
instawin is not online. instawin
Joined: 04 Jun 2013
Total Posts: 8777
02 Aug 2015 02:00 AM
i suppose you get that error because we call the function too much.. hmm..

plop this into the command bar?

local foundScripts = {}

while wait() do
local child = workspace:FindFirstChild("Script", true)
if child then table.insert(foundScripts, child) else break end
end

for i, v in pairs(foundScripts) do
print(v:GetFullName)
end

unfortunately, this snippet would only gather instances with the name of Script..
Report Abuse
instawin is not online. instawin
Joined: 04 Jun 2013
Total Posts: 8777
02 Aug 2015 02:01 AM
for i, v in pairs(foundScripts) do
print(v:GetFullName())
end

forgot the brackets when i called GetFullName lel
Report Abuse
lego555444 is not online. lego555444
Joined: 21 Sep 2008
Total Posts: 1383
02 Aug 2015 02:03 AM
spawn(function() end)

everytime you call a recurse on a new function
Report Abuse
instawin is not online. instawin
Joined: 04 Jun 2013
Total Posts: 8777
02 Aug 2015 02:04 AM
local obtainedScripts = {}

function rec(parent)
local children = parent:GetChildren()
for i, v in pairs(children) do
if v:IsA("LuaSourceContainer") then
table.insert(obtainedScripts, v)
else
spawn(function() rec(v) end)
end
end
return obtainedScripts
end

rec(workspace)

this, lego?
Report Abuse
lego555444 is not online. lego555444
Joined: 21 Sep 2008
Total Posts: 1383
02 Aug 2015 02:09 AM
Yes,try it?
Report Abuse
instawin is not online. instawin
Joined: 04 Jun 2013
Total Posts: 8777
02 Aug 2015 02:09 AM
well, i made this for him in like a couple of minutes so lel

OP, try it
Report Abuse
NewbControl114 is not online. NewbControl114
Joined: 09 May 2009
Total Posts: 10303
02 Aug 2015 02:12 AM
function geteverything(parent)
for _, child in ipairs(parent:GetChildren()) do
if child:IsA("Script") then
if child.Parent.Name == ("Part") then
print(child.Parent.Parent)
else
print(child.Parent)
end
elseif child:IsA("Model") or child:IsA("BasePart") then
geteverything(child)
end
end
end

geteverything(workspace)


this one works just as well, but isn't printing the sort of information I need.

Will yours print the full set of parents?

:(
Report Abuse
instawin is not online. instawin
Joined: 04 Jun 2013
Total Posts: 8777
02 Aug 2015 02:14 AM
local obtainedScripts = {}

function rec(parent)
local children = parent:GetChildren()
for i, v in pairs(children) do
if v:IsA("LuaSourceContainer") then
table.insert(obtainedScripts, v)
else
spawn(function() rec(v) end)
end
end
end

rec(workspace)

for i, v in pairs(obtainedScripts) do
print(v:GetFullName())
end
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
02 Aug 2015 03:10 AM
lel nobs don't know how to recurse local function!

local GetAllOf = function(p,c)
local All = {}
local recurse
recurse = function(p)
for i,v in next, p:GetChildren() do
if v:IsA(c) then
table.insert(All,v)
end
if v["GetChildren"] then -- Some services in Game do not have the GetChildren method
recurse(v)
end
end
end
recurse(p)
return All
end

local Scropts = GetAllOf(game,"LuaSourceContainer")
for _,scropt in next, Scropts do
print(scropt.Parent.Name)
end
Report Abuse
instawin is not online. instawin
Joined: 04 Jun 2013
Total Posts: 8777
02 Aug 2015 03:16 AM
cOLD mOLD on a sLATE pLATE
Report Abuse
DigitalVeer is not online. DigitalVeer
Joined: 23 Jun 2009
Total Posts: 4182
02 Aug 2015 03:27 AM
function findScript(loc, arrayToFill)
for I, v in pairs(loc:GetChildren()) do
if v:IsA("Script") then
table.insert(arrayToFill, v)
end
findScript(v, arrayToFill)
end
return arrayToFill;
end

local scripts = findScript(workspace,{})
table.foreach(scripts, function(i,v) print("Script_",i," :: ",v:GetFullName()) end);
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
02 Aug 2015 03:30 AM
*cough* I WIN I AM BETTER THAN ALL OF YOU *cough*
Report Abuse
instawin is not online. instawin
Joined: 04 Jun 2013
Total Posts: 8777
02 Aug 2015 03:44 AM
my jimmies have been rustled
:c
Report Abuse
NewbControl114 is not online. NewbControl114
Joined: 09 May 2009
Total Posts: 10303
02 Aug 2015 03:45 AM
Problem was solved eons ago by my own hand

:(
Report Abuse
instawin is not online. instawin
Joined: 04 Jun 2013
Total Posts: 8777
02 Aug 2015 03:47 AM
evry1 in dis thred got shrekt xddd
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image