|
| 10 May 2017 07:41 PM |
How can I make it choose a random child
|
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| Joined: 03 Dec 2007 |
| Total Posts: 59 |
|
|
| 10 May 2017 08:23 PM |
| Why did you make the same thread twice? |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 10 May 2017 10:12 PM |
well..that or you can do:
ref:GetChildren()[math.random()*(h-l+1)+l] |
|
|
| Report Abuse |
|
|