Hello, I would like to know how I can get the total amount of items inside a folder, randomly select them, and move them into game.Lighting. I’ve tried many different ways of scripting, but it still doesn’t seem to work the way I wish it would.
local count = 0
for i,v in pairs(workspace.Ambients.Triggers:GetChildren()) do
count = count + 1 --finds the total number of items inside folder
end
local choice = math.random(1,count) --pick a random number of items
v.Parent = game.Lighting --move items into Lighting
1 Like
Try This:
for i, item in pairs(workspace.Ambients.Triggers:GetChildren()) do
local choice = math.random(1,#item)
choice:Clone().Parent = game.Lighting
end
2 Likes
It errors with: attempt to get length of a Instance value
local items = workspace.Ambients.Triggers:GetChildren()
local randomItem = items[math.random(1, #items)]
4 Likes
That did work, but it only seems to move 1 of the items
local items = workspace.Ambients.Triggers:GetChildren()
local randomItem = items[math.random(1, #items)]
local Folder = randomItem:GetChildren()
for i, ItemInFolder in pairs(Folder) do
ItemInFolder:Clone().Parent = game.Lighting
end
2 Likes
To get the amount of items inside of a folder you can simply do:
Amount = #Folder:GetChildren()
The # is important.
2 Likes
To get a random amount of items use Random
local RandomAmount = Random.New():NextInteger(1,Amount)
Then move the items
for i,item in pairs(Folder:GetChildren()) do
if i <= RandomAmount then
item.Parent = game.Lightning
end
end
If this works please mark this as a solution, if it doesn’t, please tell me what went wrong
6 Likes
In the output, it displays: attempt to index nil with ‘NextInteger’
For this line:
local RandomAmount = Random.New:NextInteger(1,Amount)
Oops. I forgot to put the parentheses.
do this instead for that line:
local RandomAmount = Random.new():NextInteger(1,Amount)
2 Likes
This does work! Thanks to everyone who helped.