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: get random child

Previous Thread :: Next Thread 
CosmicPigeon is not online. CosmicPigeon
Joined: 14 Oct 2012
Total Posts: 5978
10 May 2017 07:41 PM
How can I make it choose a random child


Report Abuse
thefatalyti is not online. thefatalyti
Joined: 13 Jul 2009
Total Posts: 321
10 May 2017 08:11 PM
This script should work.
--some basic concepts if u want

--print(#(game.Workspace:GetChildren()))

--print((game.Workspace:GetChildren())[2])

function getRandomChildren(model)
local maxValue = #(model:GetChildren())
local randomValue = math.random(1,maxValue)
local randomSelection = (model:GetChildren())[randomValue]
print(randomSelection.Name)
end

for i=1,10,1 do
getRandomChildren(game.Workspace)
end
Report Abuse
Canmanage is not online. Canmanage
Joined: 03 Dec 2007
Total Posts: 59
10 May 2017 08:23 PM
Why did you make the same thread twice?
Report Abuse
CosmicPigeon is not online. CosmicPigeon
Joined: 14 Oct 2012
Total Posts: 5978
10 May 2017 08:25 PM
@canmanage
I wish I could delete threads
When I made the other thread I after no one was replying so I made this one with a better name that is more to the subject


Report Abuse
iiSleepy_Weirdoii is not online. iiSleepy_Weirdoii
Joined: 24 Nov 2015
Total Posts: 3
10 May 2017 08:30 PM
lol this is so random why is it like this why is it called this I barley even know what forums are so I just posted like 4 or # or # LEL
Report Abuse
ItsVortexe is not online. ItsVortexe
Joined: 11 Sep 2016
Total Posts: 7
10 May 2017 08:40 PM
get = {}
for i,v in pairs(workspace.Model:GetChildren()) do
table.insert(get, v)
int = get[math.random(1,#get)]
print(int)
end
Report Abuse
ItsVortexe is not online. ItsVortexe
Joined: 11 Sep 2016
Total Posts: 7
10 May 2017 08:42 PM
get = {}
for i,v in pairs(workspace.Model:GetChildren()) do
table.insert(get, v.Name)
int = get[math.random(1,#get)]
print(int)
end
Report Abuse
Canmanage is not online. Canmanage
Joined: 03 Dec 2007
Total Posts: 59
10 May 2017 09:11 PM
^ Why iterate through :GetChildren() just to build a table of names? Just use the size of :GetChildren() to set the upper bound on the random number then access that child directly.
Report Abuse
nullfeels is not online. nullfeels
Joined: 31 Mar 2017
Total Posts: 1215
10 May 2017 09:30 PM
Agreed @Canmanage

local children = something:GetChildren()
local randPart = nil
if (#children > 0) then
randPart = children[math.random(1,#children)]
--might want sanity check to ensure randPart is part/union
end
Report Abuse
graveyardposse is not online. graveyardposse
Joined: 23 Oct 2008
Total Posts: 2565
10 May 2017 10:11 PM
there's no need to check if the table is empty, unless you actually need to account for it:

ref:GetChildren()[math.random(l,h)]
Report Abuse
graveyardposse is not online. graveyardposse
Joined: 23 Oct 2008
Total Posts: 2565
10 May 2017 10:12 PM
well..that or you can do:

ref:GetChildren()[math.random()*(h-l+1)+l]
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